After a user manually resizes a tkinter window, it no longer shrinks to fit.
What tkinter command would revert it back to the 'shrink to fit' behavior?
I had a hard time remembering how to this, as it has been so long.
In Tcl use:
wm geometry . {}
Can one of the python people edit the answer and translate this for tkinter, thanks.
#Atlas435 wrote this code for tkinter:
import tkinter as tk
root=tk.Tk()
def normal():
root.geometry('')
b = tk.Button(root,text='shrink', command=normal)
b.pack()
root.mainloop()
Related
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.
I am working on a project using Tkinter library for making a GUI. This GUI will be displayed on a touch screen using raspberry pi 3.
I want to prevent user from exiting or minimising the program.
Is there any way to disable or remove the title bar? Or is there a better way to achieve this?
Since you mentioned a raspberry pi I suppose you are using Linux. In this case you can use root.attributes('-type', 'dock') (assuming your Tk instance is called root). This way, your window will have no decoration (so no close or minimize buttons) and will be always on top. If you don't want it always on top, you can use type 'splash' instead. In any case, you will need to use focus_force to be able to get keyboard focus.
import tkinter as tk
root = tk.Tk()
root.attributes('-type', 'dock')
root.geometry('200x200')
tk.Entry(root).pack()
root.focus_force()
root.mainloop()
Otherwise, you can prevent the window from being closed by setting the 'WM_DELETE_WINDOW' protocol and redisplay the window each time it is minimized:
import tkinter as tk
root = tk.Tk()
def unmap(event):
if event.widget is root:
root.deiconify()
root.protocol('WM_DELETE_WINDOW', lambda: None) # prevent closing
root.bind('<Unmap>', unmap) # redisplay window when it's minimized
root.mainloop()
root = tk.Tk()
root.wm_attributes('-type', 'splash')
For more details go to this link: Remove titlebar without overrideredirect() using Tkinter?
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.
This simple code is not working.
I mean its running without errors but
is not showing any gui window for text entry.
from Tkinter import *
from tkMessageBox import *
root=Tk()
Label(root,text="first").grid(row=0)
Label(root,text="second").grid(row=2)
e1=Entry(root)
e1.grid(row=0,column=2)
e2=Entry(root)
e2.grid(row=2,column=3)
def info():
s=showinfo(title="wish",message=e1.get()+''+"welcome to python")
Button(root,text="ok",command=info).pack()
root.mainloop()
You can't use grid and pack on two widgets owned by the same parent. This causes the geometry manager to loop forever.
Change your Button positioning to:
Button(root,text="ok",command=info).grid(row=3, column=0)
(or whatever row/column you want it to be in).
Result:
I'm trying to get a tkinter message widget to make the words move when I resize the window. Right now, the window is a small block, and the line of text is an ugly block. How can I make it expand. This is the code I have.
root = Tk()
Message(text="This is a Tkinter message widget. Pretty exiting, huh? I enjoy Tkinter. It is very simple.").pack()
root.mainloop()
I hope you understand my question. Thanks.
You need to set the width of the Message text when you resize the window. As far as I know, there is no way to tell the Message widget do that automatically, so you're stuck with using a callback:
from tkinter import Tk, Message
root = Tk()
m = Message(text="This is a Tkinter message widget. Pretty exiting, huh? I enjoy Tkinter. It is very simple.")
m.pack(expand=True, fill='x')
m.bind("<Configure>", lambda e: m.configure(width=e.width-10))
root.mainloop()