resizable window frame in tkinter - python

I created a window with tkinter for my programm but I want it to resize itself when the user resizes the window. Right now when I increase or decrease the size of the window the widgets stay at the same position and don't in-/decrease in size.
Here is my code:
import tkinter as tk
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.check1 = tk.IntVar()
self.fileopenname1 = tk.StringVar()
self.entrystring = tk.IntVar()
self.menubar = tk.Menu(self)
self.create_widgets()
def create_widgets(self):
self.filemenu=tk.Menu(self.menubar,tearoff=0)
self.menubar.add_cascade(label="File",menu=self.filemenu)
self.filemenu.add_command(label="Exit",command = root.destroy)
self.helpmenu=tk.Menu(self.menubar,tearoff=0)
self.programmstart = tk.Button(self, text = "Start Program")
self.programmstart.grid(row=10,column=8,sticky = "W")
self.checkbutton1 = tk.Checkbutton(self, text = "Drehzahl und Drehmoment",variable=self.check1,onvalue=1,offvalue=0)
self.checkbutton1.grid(row=0,column=0,columnspan=3,sticky = "W")
self.leer1 = tk.Label(self,text=" ") #erzeugt leere Zelle, sonst ist startbutton links
self.leer1.grid(row=0,column=3,columnspan=5)
self.leer2 = tk.Label(self,text=" ")
self.leer2.grid(row=5,column=8,rowspan=2)
self.leer3 = tk.Label(self,text=" ")
self.leer3.grid(row=9,column=9)
self.inputpathdisplay = tk.Label(self,textvariable=self.fileopenname1,bg="white",width=60)
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
self.inputpathdisplaylabel = tk.Label(self,text="Inputfile")
self.inputpathdisplaylabel.grid(row=0,column=8,columnspan=3)
root = tk.Tk()
app = Graphicaluserinterface(master=root)
app.master.title("Programm")
app.master.minsize(800,300)
root.config(menu=app.menubar)
app.mainloop()

You need to configure the weight on your grid's rows and columns. The following is a refactored version of your code that should act a little more as you were hoping. Please continue development from this code:
#! /usr/bin/env python3
import tkinter
from tkinter.constants import *
class GraphicalUserInterface(tkinter.Frame):
#classmethod
def main(cls):
root = tkinter.Tk()
root.title('Program')
root.minsize(560, 105)
gui = cls(root)
gui.grid(row=0, column=0, sticky=NSEW)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root['menu'] = gui.menubar
cls.create_toplevel(root, 'First Toplevel')
root.mainloop()
#staticmethod
def create_toplevel(root, window_title):
window = tkinter.Toplevel(root)
window.title(window_title)
window.minsize(560, 105)
def __init__(self, master=None):
super().__init__(master)
self.check_value = tkinter.BooleanVar()
self.filename = tkinter.StringVar()
self.menubar = tkinter.Menu(self)
self.file_menu = tkinter.Menu(self.menubar, tearoff=FALSE)
self.help_menu = tkinter.Menu(self.menubar, tearoff=FALSE)
self.program_start = tkinter.Button(
self, text='Start Program',
command=lambda: self.create_toplevel(self.master, 'Another Window')
)
self.check_button = tkinter.Checkbutton(
self, text='Speed ​​& Torque', variable=self.check_value,
onvalue=True, offvalue=False
)
self.input_path_display = tkinter.Label(
self, textvariable=self.filename, bg='white', width=60
)
self.input_path_display_label = tkinter.Label(self, text='Input File')
self.create_widgets()
def create_widgets(self):
self.menubar.add_cascade(label='File', menu=self.file_menu)
self.file_menu.add_command(label='Exit', command=self.master.destroy)
pad = dict(padx=5, pady=5)
self.check_button.grid(row=0, column=0, **pad)
self.input_path_display_label.grid(row=0, column=1, sticky=EW, **pad)
self.input_path_display.grid(row=1, column=1, sticky=NSEW, **pad)
self.program_start.grid(row=2, column=1, sticky=EW, **pad)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
if __name__ == '__main__':
GraphicalUserInterface.main()

Related

Tkinter grid doesn't expand when the windows change its size

I'm new using Tkinter, I already have this problem, I was thinking the problem was the column and row configure, but that doesn't change anything.
This is what I get:
This is my code:
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.grid()
self.grid_rowconfigure(0, weight=1)
self.grid_rowconfigure(1, weight=1)
self.grid_rowconfigure(2, weight=1)
self.grid_rowconfigure(3, weight=1)
self.grid_rowconfigure(4, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
self.create_widgets()
def create_widgets(self):
self.label = Label(self, justify=LEFT)
self.label["text"] = "Número:"
self.label.grid(column=0, row=0, sticky="e")
self.input = Entry(self)
self.input.grid(column=1, row=0, sticky="we")
self.baseValue = StringVar()
self.baseValue.set(basesNames[0]) # default value
self.label2 = Label(self)
self.label2["text"] = "Base:"
self.label2.grid(column=0, row=1, sticky="e")
self.menuNumber = ttk.Combobox(self, textvariable=self.baseValue)
self.menuNumber['values'] = basesNames
self.menuNumber.grid(column=1, row=1)
self.label2 = Label(self)
self.label2["text"] = "Base de resultado: "
self.label2.grid(column=0, row=2, sticky="e")
self.baseValueResult = StringVar()
self.baseValueResult.set(basesNames[0]) # default value
self.menuNumberResult = ttk.Combobox(self, textvariable=self.baseValueResult)
self.menuNumberResult['values'] = basesNames
self.menuNumberResult.grid(column=1, row=2)
self.convert = ttk.Button(self, text="Convertir", command=self.convertNumber)
self.convert.grid(row = 4, columnspan=2, sticky="sn")
self.result = Label(self)
self.result["text"] = "Your result is: "
self.result.grid(row=3, columnspan=2, sticky="ew")
root = Tk()
app = Application(master=root)
app.master.title("Conversor de bases")
app.master.maxsize(1000, 400)
app.mainloop()
Also, I checked if the sticky param was necessary but that doesn't change neither
Your widgets are wrapped inside the frame Application. So you need to make the frame expands as well.
from tkinter import *
from tkinter import ttk
basesNames = ["a","b","c","d","e"]
class Application(Frame):
def __init__(self, master=None):
...
root = Tk()
app = Application(master=root)
app.master.title("Conversor de bases")
app.master.maxsize(1000, 400)
root.rowconfigure(0,weight=1)
root.columnconfigure(0,weight=1)
root.mainloop()

the tkinter widgets in my classes are not displaying

I'm trying to write a code that contains multiple pages and can be switched to when a button is clicked on. it worked initially but my widgets are not displaying, and there is neither a warning or an error message. Secondly, what is the difference between using tk and tk.TK?
from tkinter import *
import tkinter as tk
class moreTab(tk.Tk):
def __init__(self):
Tk.__init__(self)
self.geometry("1200x600")
container = Frame(self, bg='#c9e3c1')
container.pack(side = "top", fill = 'both', expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for q in (pageone, widget):
frame = q(container,self)
self.frames[q] = frame
frame.place(x= 0,y = 0)
self.raise_frame(pageone)
def raise_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class widget(Frame):
def __init__(self, master, control):
Frame.__init__(self, master)
lab = tk.Label(self, text="main page")
lab.place(x = 10, y = 40)
but = tk.Button(self, text='visit start page', command=lambda:
control.raise_frame(pageone))
but.place(x = 10, y = 70)
class pageone(Frame):
def __init__(self, master, control):
Frame.__init__(self,master)
lab = Label(self, text = 'welcome to Game Analysis')
lab.place(x = 10, y = 10)
but = Button(self, text = "Start", command = lambda:
control.raise_frame(widget))
but.place(x = 10, y = 20)
app = moreTab()
app.mainloop()
It turns the issue was that you were using place(). Use the grid geometry manager. Using both import tkinter as tk and from tkinter import * is meaningless. Use one and be consistent. If you use the latter, you have everything available, hence you will write, say Button(...). But if you use the former, you will have to refer each widget like tk.Button(...).
import tkinter as tk
class moreTab(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry("1200x600")
container = tk.Frame(self, bg='#c9e3c1')
container.pack(side = "top", fill = 'both', expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for q in (pageone, widget):
frame = q(container, self)
self.frames[q] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.raise_frame(pageone)
def raise_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class widget(tk.Frame):
def __init__(self, master, control):
tk.Frame.__init__(self, master)
lab = tk.Label(self, text="main page")
lab.grid(row=0, column=0, padx=10, pady=10)
but = tk.Button(self, text='visit start page', command=lambda: control.raise_frame(pageone))
but.grid(row=1, column=0, padx=10, pady=10)
class pageone(tk.Frame):
def __init__(self, master, control):
tk.Frame.__init__(self, master)
lab = tk.Label(self, text = 'welcome to Game Analysis')
lab.grid(row=0, column=0, padx=10, pady=10)
but = tk.Button(self, text = "Start", command = lambda: control.raise_frame(widget))
but.grid(row=1, column=0, padx=10, pady=10)
app = moreTab()
app.mainloop()

Retriving output from two entry dialog box with python

I have the following code to generate a two entry input dialog box and a button to close the "app".
However I cannot cannot get the values out of the outputs. They always come out as x and N. The code was not developed by me since I am a beginner with python. Can anyone give me hand with this?
from tkinter import Tk, Text, TOP, BOTH, X, N, LEFT, RIGH
from tkinter.ttk import Frame, Label, Entry, Button
class SimpleDialog(Frame):
def __init__(self):
super().__init__()
self.output1 = ""
self.output2 = ""
self.initUI()
def initUI(self):
self.master.title("Simple Dialog")
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack()
lbl1 = Label(frame1, text="Input1", width=6)
lbl1.pack(side=LEFT, padx=5, pady=10)
self.entry1 = Entry(frame1)
self.entry1.pack(padx=5, expand=True)
frame2 = Frame(self)
frame2.pack()
lbl2 = Label(frame2, text="Input2", width=6)
lbl2.pack(side=LEFT, padx=5, pady=10)
self.entry2 = Entry(frame2)
self.entry2.pack(padx=5, expand=True)
frame3 = Frame(self)
frame3.pack()
btn = Button(frame3, text="Submit", command=self.onSubmit)
btn.pack(padx=5, pady=10)
def onSubmit(self):
self.output1 = self.entry1.get()
self.output2 = self.entry2.get()
self.quit()
def main():
# This part triggers the dialog
root = Tk()
root.geometry("250x150+300+300")
app = SimpleDialog()
root.mainloop()
user_input = (app.output1, app.output2)
try:
root.destroy()
except:
pass
return user_input
if __name__ == '__main__':
main()
kind regards!

Tkinter: Entry box value doesn't store into StringVar() object

I'm trying to see whether or not a value entered in the Entry widget is stored inside the StringVar() object but when I print the length of the string value object, it says 0.
class POS(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self, *args, **kwargs)
"""
some code to build multiple frames here
"""
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
frame5 = Frame(self, bg = "pink")
frame5.pack(fill = BOTH)
frame5Label1 = tk.Label(frame5, text = "Product Bar Code", font = NORMAL_FONT)
frame5Label1.grid(row = 0, column = 0, padx=5, pady=5, sticky = W)
barCode = StringVar()
frame5EntryBox = ttk.Entry(frame5, textvariable = barCode, width = 40)
frame5EntryBox.grid(row = 0, column = 1, padx = 5, pady = 5)
frame5Button = ttk.Button(frame5, text = "Add Item", command = lambda: updateCustomerList(barCode))
frame5Button.grid(row = 0, column = 2, padx = 130, pady = 10)
def updateCustomerList(barCode):
print(len(barCode.get()))
app = POS()
app.geometry("700x700")
app.resizable(False,False)
app.mainloop()
Minimal working example which displays length of StringVar assigned to Entry after pressing Button
import tkinter as tk
# --- functions ---
def check():
print('len:', len(var.get()))
# --- main ---
root = tk.Tk()
var = tk.StringVar()
ent = tk.Entry(root, textvariable=var)
ent.pack()
but = tk.Button(root, text="Check", command=check)
but.pack()
root.mainloop()
mainloop displays window with widgets so using len() before starting mainloop makes no sense because var is still empty.
EDIT: with your class and function outside class
import tkinter as tk
from tkinter import ttk
# --- classes ---
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
frame5 = tk.Frame(self, bg="pink")
frame5.pack(fill="both")
frame5Label1 = tk.Label(frame5, text="Product Bar Code", font="NORMAL_FONT")
frame5Label1.grid(row=0, column=0, padx=5, pady=5, sticky="w")
barCode = tk.StringVar()
frame5EntryBox = ttk.Entry(frame5, textvariable=barCode, width=40)
frame5EntryBox.grid(row=0, column=1, padx=5, pady=5)
frame5Button = ttk.Button(frame5, text="Add Item", command=lambda:updateCustomerList(barCode))
frame5Button.grid(row=0, column=2, padx=130, pady=10)
# --- functions ---
def updateCustomerList(barCode):
print(len(barCode.get()))
# --- main ---
root = tk.Tk()
main = MainPage(root, root)
main.pack()
root.mainloop()
Or using self. and putting function inside class as method
import tkinter as tk
from tkinter import ttk
# --- classes ---
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
frame5 = tk.Frame(self, bg="pink")
frame5.pack(fill="both")
frame5Label1 = tk.Label(frame5, text="Product Bar Code", font="NORMAL_FONT")
frame5Label1.grid(row=0, column=0, padx=5, pady=5, sticky="w")
self.barCode = tk.StringVar()
frame5EntryBox = ttk.Entry(frame5, textvariable=self.barCode, width=40)
frame5EntryBox.grid(row=0, column=1, padx=5, pady=5)
frame5Button = ttk.Button(frame5, text="Add Item", command=self.updateCustomerList)
frame5Button.grid(row=0, column=2, padx=130, pady=10)
def updateCustomerList(self):
print(len(self.barCode.get()))
# --- functions ---
# --- main ---
root = tk.Tk()
main = MainPage(root, root)
main.pack()
root.mainloop()

How can I place the output of different button functions in tkinter in a particular frame?

I'm new to Python.
On clicking the button "check connection", I want the message to be displayed in the tkinter window itself, rather than a message box... I want the output of every button function in the same place in the tkinter.
from Tkinter import *
import subprocess
root = Tk()
root.geometry("850x500+300+300")
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Windows")
#self.style = Style()
#self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
lbl = Label(self, text="Windows")
lbl.grid(sticky=W, pady=4, padx=5)
abtn = Button(self, text="Check Wired Connections",command = self.checkconnections)
abtn.grid(row=1, column=3)
cbtn = Button(self, text="Check Wireless Connections")
cbtn.grid(row=3, column=3, pady=4)
obtn = Button(self, text="OK")
obtn.grid(row=5, column=3)
self.v = StringVar()
messagelabel=Label(self, textvariable=self.v)
messagelabel.grid(row=1, column=0, pady=0)
def checkconnections(self):
p = subprocess.call('nm-tool')
print p
self.v.set(p)
def main():
app = Example(root)
root.mainloop()
main()
then create label in your class
class Example(Frame):
.....
self.v = StringVar()
messagelabel=Label(self, textvariable=v).pack()
self.v.set("")
......
def checkconnections():
p1 = subprocess.check_output('nm-tool' )
self.v.set('nm-tool', p1)
from Tkinter import *
import subprocess
root = Tk()
root.geometry("850x500+300+300")
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Windows")
#self.style = Style()
#self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
lbl = Label(self, text="Windows")
lbl.grid(sticky=W, pady=4, padx=5)
abtn = Button(self, text="Check Wired Connections",command = self.checkconnections)
abtn.grid(row=1, column=3)
cbtn = Button(self, text="Check Wireless Connections")
cbtn.grid(row=3, column=3, pady=4)
obtn = Button(self, text="OK")
obtn.grid(row=5, column=3)
self.v = StringVar()
messagelabel=Label(self, textvariable=self.v)
messagelabel.grid(row=1, column=0, pady=0)
def checkconnections(self):
self.v.set("hai")
def main():
app = Example(root)
root.mainloop()
main()

Categories

Resources