Set width and height of object inheriting from frame class in tkinter - python

I am trying to build a GUI calculator with tabbed pages using tkk.Notebook. The different pages consist of objects inheriting from the ttk.Frame widget. I am able to create the frames but they end up being very small and the widgets I have placed on them overlap and impede on each other. Using the width and height options with the frames does not seem to change the dimensions at all either. I am fairly new to object oriented programming, so I think the issue has to do with how I am creating the frames, but I cant seem to figure it out.
import tkinter as tk
from tkinter import ttk
from ttkthemes import ThemedStyle
class DimFrame(ttk.Frame):
def __init__(self, master):
super().__init__(master)
self.length_label = ttk.Label(self, text='Length: ')
self.length_label.grid(row=0)
self.length = float()
self.length_entry = ttk.Entry(self, textvariable=self.length, width=10)
self.length_entry.grid(column=1,row=0)
...more methods and widgets, etc...
def main():
root = tk.Tk()
root.geometry('1000x500')
s = ttk.Style()
style = ThemedStyle(root)
style.set_theme('black')
notebook = ttk.Notebook(root)
notebook.grid()
dimensions_frame = DimFrame(root)
dimensions_frame.grid()
notebook.add(dimensions_frame, text='Room Dimensions')
root.mainloop()
if __name__ == '__main__':
main()

Related

How to make the ttk.COMBOBOX arrow size larger in Tkinter

I am making a GUI program using Raspberry Pi.
This GUI is made with Tkinter and works only on the touch screen, no mouse and keyboard.
However, the arrow buttons on the COMBOBOX are too small to touch with your fingers.
I used "tkinter.font" to increase the font size, but the font size only increases and the arrow buttons do not increase in size.
How can I increase the size of this arrow button?
How to apply a style doesn't work. My code is below. please help me
import tkinter.ttk
import tkinter.font
class Application:
def __init__(self, parent):
self.parent = parent
self.data = [1,2,3]
self.bigfont = tkinter.font.Font(family="Helvetica",size=30)
root.option_add("*TCombobox*Listbox*Font", self.bigfont)
self.combo()
def combo(self):
self.style = tkinter.ttk.Style()
self.style.configure('W.TCombobox', arrowsize = 1000)
self.cBox = tkinter.ttk.Combobox(self.parent, values = self.data, style = 'W.TCombobox', font=self.bigfont)
self.cBox.current(0)
self.cBox.pack()
if __name__ == '__main__':
root = tkinter.Tk()
root.title("COMBOBOX TEST")
root.geometry("640x400+100+100")
app = Application(root)
root.mainloop()

simple window with an enter field

i'm a new programmer and there are certainly several errors but this shouldn't be difficult to spot. I need to create a simple window with a field named "Concorrente 1:" and an entry field displayed by function named lacopertina(). I don't understand where is the error:
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
class schermoiniziale(tk.Frame):
def lacopertina():
print(gio1)
#return (tot1)
def __init__(self):
global gio1
#tot1=0
#schermo1=Tk()
self.gio1=tk.StringVar()
lab1=ttk.Label(self, text="Concorrente 1:")
lab1.pack()
ent1=ttk.Entry(self, textvariable=self.gio1)
ent1.pack()
pulsante = ttk.Button(self, text="Inizio", textvariable=self.gio1, command=self.lacopertina)
pulsante.pack()
def main():
schermoiniziale().mainloop()
if __name__== "__main__":
main()
I would suggest you to go through some tutorials on Python OOP.
I have modified your code as below with some comment:
# avoid using wildcard import
import tkinter as tk
from tkinter import ttk
class schermoiniziale(tk.Frame):
def __init__(self, master, **kw):
# need to call __init__() of inherited class
super().__init__(master, **kw)
self.gio1 = tk.StringVar()
lab1 = ttk.Label(self, text="Concorrente 1:")
lab1.pack()
ent1 = ttk.Entry(self, textvariable=self.gio1)
ent1.pack()
# removed textvariable=self.gio1 as I think you actually don't need it
pulsante = ttk.Button(self, text="Inizio", command=self.lacopertina)
pulsante.pack()
def lacopertina(self):
# use .get() to get the content of a StringVar
print(self.gio1.get())
def main():
# need to create the root window before creating other widget
root = tk.Tk()
# pass root window as the parent of the widget
frame = schermoiniziale(root)
frame.pack()
# start the tkinter mainloop
root.mainloop()
if __name__== "__main__":
main()

tkinter: Why am I getting a small window plus my main window and gridding is off? __init__ problem?

Creates two windows and gridding is not correct. Some additional comments in the code initiation.
I have used this approach, without the super init with no problem, many times.
Advice appreciated.
Thanks
# timhockswender#gmail.com
import tkinter as tk
from tkinter import ttk
class constants_page(tk.Frame):
def __init__(self):
super(constants_page, self).__init__() # from stackoverflow
# if not used error = 'constants_page' object has no attribute 'tk'
# if used, another tiny window is opened
# in addtion to the constants_page
self.constants_page = tk.Tk()
self.constants_page.geometry("1000x500") #width*Length
self.constants_page.title("Owen's Unit Conversion App")
self.constants_page.configure(background='light blue')
self.CreateWidgets()
def CreateWidgets(self):
self.value_label = ttk.Label(self.constants_page,text="Value----->" , width =10 )
self.value_label.grid(row=0, column=1, columnspan=1, sticky='nse')
# Problem: not gridding properly
self.title_label = ttk.Label(self.constants_page, text="Important Physical Constants",
anchor=tk.CENTER, font=("Arial",20)).grid(row=2, columnspan=2)
for r in range(2):
self.constants_page.rowconfigure(r, weight=1, uniform='row')
for c in range(2):
self.constants_page.columnconfigure(c, weight=1 )
def Show_Page():
# Create the entire GUI program
program = constants_page()
program.mainloop()
if __name__ == "__main__":
Show_Page()
The super call expects you to provide a root window (an instance of tk.Tk()). If you don't provide one it defaults to the first root window opened, and if none has been opened yet then it helpfully opens one for you. A few lines later you open a second one yourself.
The easy fix is to remove the self.constants_page = tk.Tk() line. The proper fix is to make the Tk() instance outside of the class and pass it in. This allows you to use the Frame class itself to lay out widgets (use self instead of self.constants_page). Try this:
import tkinter as tk
from tkinter import ttk
class constants_page(tk.Frame):
def __init__(self, master=None, **kwargs):
super().__init__(master, **kwargs)
master.geometry("1000x500") #width*Length
master.title("Owen's Unit Conversion App")
self.configure(background='light blue')
self.CreateWidgets()
def CreateWidgets(self):
self.value_label = ttk.Label(self,text="Value----->" , width =10 )
self.value_label.grid(row=0, column=1, columnspan=1, sticky='nse')
self.title_label = ttk.Label(self, text="Important Physical Constants",
anchor=tk.CENTER, font=("Arial",20)).grid(row=2, columnspan=2)
for r in range(2):
self.rowconfigure(r, weight=1, uniform='row')
for c in range(2):
self.columnconfigure(c, weight=1 )
def Show_Page():
# Create the entire GUI program
program = tk.Tk()
win = constants_page(program)
win.pack()
program.mainloop()
if __name__ == "__main__":
Show_Page()

How can I place a widget at the very bottom of a tkinter window when positioning with .grid()?

I am aware that you cannot use different types of geometry managers within the same Tkinter window, such as .grid() and .pack(). I have a window that has been laid out using .grid() and I am now trying to add a status bar that would be snapped to the bottom of the window. The only method I have found online for this is to use .pack(side = BOTTOM), which will not work since the rest of the window uses .grid().
Is there a way that I can select the bottom of the window to place widgets from when using .grid()?
from tkinter import *
from tkinter.ttk import *
import tkinter as tk
class sample(Frame):
def __init__(self,master=None):
Frame.__init__(self, master)
self.status = StringVar()
self.status.set("Initializing")
statusbar = Label(root,textvariable = self.status,relief = SUNKEN, anchor = W)
statusbar.pack(side = BOTTOM, fill = X)
self.parent1 = Frame()
self.parent1.pack(side = TOP)
self.createwidgets()
def createwidgets(self):
Label(self.parent1,text = "Grid 1,1").grid(row = 1, column = 1)
Label(self.parent1,text = "Grid 1,2").grid(row = 1, column = 2)
Label(self.parent1,text = "Grid 2,1").grid(row = 2, column = 1)
Label(self.parent1,text = "Grid 2,2").grid(row = 2, column = 2)
if __name__ == '__main__':
root = Tk()
app = sample(master=root)
app.mainloop()
So using labels since I was kinda lazy to do other stuff, you can do frames to ensure that each section of your window can be packed/grid as required. Frames will be a useful tool for you to use when trying to arrange your widgets. Note that using a class can make things a little easier when deciding your parents. So imagine each frame is a parent and their children can be packed as required. So I would recommend drawing out your desired GUI and see how you will arrange them. Also if you want to add another frame within a frame simply do:
self.level2 = Frame(self.parent1)
You can check out additional settings in the docs
http://effbot.org/tkinterbook/frame.htm
PS: I am using a class hence the self, if you don't want to use classes then its okay to just change it to be without a class. Classes make it nicer to read though
Just give it a row argument that is larger than any other row. Then, give a weight to at least one of the rows before it.
Even better is to use frames to organize your code. Pack the scrollbar on the bottom and a frame above it. Then, use grid for everything inside the frame.
Example:
# layout of the root window
main = tk.Frame(root)
statusbar = tk.Label(root, text="this is the statusbar", anchor="w")
statusbar.pack(side="bottom", fill="x")
main.pack(side="top", fill="both", expand=True)
# layout of the main window
for row in range(1, 10):
label = tk.Label(main, text=f"R{row}")
label.grid(row=row, sticky="nsew")
main.grid_rowconfigure(row, weight=1)
...

Can't get create_window to work on a Canvas. Tkinter Python 3.6

I have made my own widget called 'InventorySlot' and I need the widgets within my custom one to be made onto a Canvas instead of using 'grid' or 'pack'.
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
class Main:
def __init__(self):
self.root = Tk()
self.root.geometry('500x500')
self.test = InventorySlot(self.root)
self.test.grid()
class InventorySlot(tk.Frame):
def __init__(self,parent,*args,**kwargs):
tk.Frame.__init__(self,parent)
self.options = {}
self.options.update(kwargs)
self.slot = tk.Label(self,height=3,width=6,text='',relief=SUNKEN,bg='#8b8b8b',bd=4,padx=1,pady=0)
self.canvas = Canvas(self)
self.canvas.create_window(10,10,window=self.slot)
self.canvas.grid()
MainTk = Main()
MainTk.root.mainloop()
All it shows is a blank Canvas
You need to create the label after creating the canvas. The order of creation determines the stacking order (ie: the z-index). The label on s there, it’s just behind the canvas.

Categories

Resources