tkinter optionmenu swapping place - python

After googling and searching in SO, I come here.
I am making a program, but when I run it, the optionMenu widgets go to the end of the display, despite being set on the grid on the program. Here's a relevant example:
from tkinter import *
root=Tk()
root.title("Generador documentos")
app=Frame(root)
app.grid()
sexoPat=Label(app, text ="Gender")
sexoPat.grid(row=0,column=0)
var1 = StringVar()
sexoPatDrop= OptionMenu(root,var1,'Male','Female')
sexoPatDrop.grid(row=1,column=0)
sexoPatCheck=var1.get()
nombPat=Label(app, text ="name here")
nombPat.grid(row=2,column=0)
nombPatTXT=Entry(app)
nombPatTXT.grid(row=3,column=0)

In this sample code, I needed to write app instead of root in OptionMenu(root,...)
It was furas who gave me the answer.

Related

How do i resize the pop-up from simpledialog.askstring in tkinter?

While using the simplesialog.askstring is good and all, I would like to resize the pop-up window and also resize the width of the text input.
(sample code)
from tkinter import *
from tkinter import simpledialog
prompts = ["name", "age", "height", "wheight"]
root = Tk()
for p in prompts:
answer = simpledialog.askstring(p, root)
print(answer)
I have looked at different documentation, but could not seem to spot how to do it.
If you just want to make the dialog window wider, then add extra tabs at the end of your prompt string.
For example:
table_name = tk.simpledialog.askstring("Create Table", "Enter a name for the new table.\t\t\t")
Possible Copy of Increase tkSimpleDialog window size?
My understanding is that tkinter.simpledialog is a very simple and easy-to-use dialog box with very few options(basically title and prompt).
No answer here is going to look as 'clean' as your code, unless you modify the tkinter module.(unless you get away from tkinter)
I would recommend either simply using the given simpledialog one, or trying something like the easygui enterbox for a different look, or making a simple GUI with tkinter.

tkinter fill=Y does not work with Button [duplicate]

I am familiarizing myself with Tkinter, and I am attempting to write a very simple program, which displays a button in a window, using the pack geometry manager.
I was experimenting with various configuration options for pack(), such as expand, fill, and side, and I've run into a peculiar problem. I have written the following code:
from Tkinter import *
root = Tk()
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()
The problem is that the button expands to fill the window in the horizontal direction, but not the vertical direction. This is the same result that I get if instead of specifying fill=BOTH I use fill=X. In addition, if I specify instead fill=Y the button does not expand in either direction. Something seems to be going wrong with the fill in the vertical direction, and I cannot figure out what it might be.
I attempted to Google this problem and surprisingly found no mention of this happening to anyone else. I am using a Mac with OS X Yosemite and running python 2.7.5. I also attempted to compile with python 3.4.1 and saw no change.
Edit:
Based off of the answer and comments below, it is clear that there is nothing wrong with my code, because it seems to work on other machines. If not an error in the code, does anyone know what could possibly be causing the button to not stretch vertically when I run the above code?
This is a feature of native buttons on OSX. Buttons on OSX will be a fixed height and will not expand vertically. There is nothing you can do, short of using a different widget such as a label.
try running this code to see the behavior of fill and expand
from Tkinter import *
root = Tk()
root.geometry("500x500")
widget = Button(root, text='text1')
widget.pack(fill=X, expand=1)
widget = Button(root, text='text2')
widget.pack(fill=Y, expand=1)
widget = Button(root, text='text3')
widget.pack(fill=BOTH, expand=1)
root.mainloop()
Argument fill does fill in vertical direction as well
I am also beginner, defining geometry for fill was missing in your code as given below:
from Tkinter import *
root = Tk()
root.geometry("600x400")
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()

string variable not working with

I have my main window, which i refer to as mainroot, and when i press a button the secondary window root should appear
in root there is an entry and i want it to be assigned to "hohoho" whenever i press anykey, so i tied it to KeyRelease event.
All i mentioned above is working when there is no mainroot, As in this code:
import tkinter as Tk
# mainroot=Tk.Tk()
root=Tk.Tk()
root.geometry("200x200")
myvar=Tk.StringVar()
entry=Tk.Entry(root,)
entry.pack()
entry.configure(textvariable=myvar)
entry.bind('<KeyRelease>',lambda event:printing())
def printing():
myvar.set("hohohohohohoho")
print('myvar is',myvar.get())
print('value is',entry.get())
root.mainloop()
# mainroot.mainloop()
but if you commented the root.mainloop() and uncommented root.mainloop() and # mainroot.mainloop() the text shown in entrystops following myvar.
what i found is that myvar is not working properly as it should.
anyone have any idea what am doing wrong or is this bug in tkinter or what?????
I know that entry.trace() is more elegant but i didn't use it because i am not very familiar with it.
thank you
any hint will be appreciated

ttk OptionMenu does not fit into its bounding box

I am facing a problem creating a Tkinter-application under Windows, using python 2.7. Basically, when I create an OptionMenu, its right corner (where a down button indicates that something happens when you click there) is truncated in the middle.
The following code reproduces the issue:
from Tkinter import Tk, StringVar
from ttk import OptionMenu
root = Tk()
options = list('ABC')
var = StringVar(value='A')
om = OptionMenu(root, var, var.get(), *options)
om.config(width=25)
om.pack()
root.mainloop()
The result looks on my computer like this:
I have played around with the padx and ipadx keywords of the packing layout manager and also tried a grid layout instead. None of them lets me see the down-arrow completely.
I appreciate your helpful comments on this issue.
The same happens to me on Windows 7 but not on XP, both using Python 2.7. I have found a bug report which states is should be fixed in Tk 8.5.8. Updating Tcl/Tk in Python seems to be very complicated though
The fix in question is for one of the script files shipped in the tk library. You could modify your local copy of vistaTheme.tcl to match this. In later versions I think it does actually request the size from the system properly but this should work if you are forced to use an older version of Tk.
You can find the path using:
from Tkinter import Tk
tk = Tk()
tk.eval("set tk_library")
and then edit the /ttk/vistaTheme.tcl file. I've got python3 here and it seems to have come with Tk 8.6.1 so has this fixed already.

How to correctly use Control Variable in Tkinter?

I just started to look into tkinter to develop some simple GUI applications. However, I had a hard time figuring out how the control variables are used. I simply want to retrieve the status of a checkbutton(whether it is checked) and the text of an entry, but using the control variable seems not doing the job. I know my_entry.get() can also be used to get the text, but since most of the tutorials use control variables, I believe it is worth learning to use it correctly... Really appreciate your help!
here is the sample code I had:
import tkinter as tk
def checkbtn_callback():
print(btn_state.get())
print(entry_text.get())
top = tk.Tk()
btn_state = tk.IntVar()
checkbtn=tk.Checkbutton(top,text='testbutton',variable=btn_state,command=checkbtn_callback)
checkbtn.grid()
entry_text = tk.StringVar()
entry = tk.Entry(top, text='text entry',textvariable=entry_text)
entry.grid()
tk.mainloop()

Categories