Reputation: 129
I'm trying to make a financial calculator for myself. Till now I didn't have any major problems, but now I'm stuck and can't figure out what's wrong. I'm new in this world so don't be mad about my lame coding skills haha! I made sqlite database with python and in this step I'm trying to pull one column and make button for each value dynamically. I succeed to make that step but my TopLevel window is not resizing how it should. That problem is caused because I want that TopLevel to be opened at same spot as root window, but I can't figure out where I'm making mistake. Here is my code, I deleted most of unimportant widgets and I made automatic input into db so you guys can check that dynamic button creation. Hope you understood what my problem is about if you need more explanation I will try my best. Most of code variables are on my language, hope that's not a problem and I don't know why indenting is messed up when I paste the code.
from tkinter import *
import sqlite3
conn = sqlite3.connect('Test.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS OTPLATA_NA_RATE
([ID] INTEGER PRIMARY KEY,[Opis] text, [Iznos_rate] integer, [Broj_rata] text, [Datum_kupnje] text)''')
c.execute("INSERT INTO OTPLATA_NA_RATE VALUES (?, ?, ?, ?, ?);", (None, "a", 500, "2", "2021-08-10"))
c.execute("INSERT INTO OTPLATA_NA_RATE VALUES (?, ?, ?, ?, ?);", (None, "b", 500, "2", "2021-08-10"))
c.execute("INSERT INTO OTPLATA_NA_RATE VALUES (?, ?, ?, ?, ?);", (None, "c", 500, "2", "2021-08-10"))
c.execute("INSERT INTO OTPLATA_NA_RATE VALUES (?, ?, ?, ?, ?);", (None, "d", 500, "2", "2021-08-10"))
c.execute("INSERT INTO OTPLATA_NA_RATE VALUES (?, ?, ?, ?, ?);", (None, "e", 500, "2", "2021-08-10"))
c.execute("INSERT INTO OTPLATA_NA_RATE VALUES (?, ?, ?, ?, ?);", (None, "f", 500, "2", "2021-08-10"))
class FinancialCalc:
def __init__(self, master):
# Start window:
self.master = master
self.master.title("Kalkulator troška")
self.master.geometry("300x300")
self.master.resizable(False, False)
self.otplata_na_rate_button = Button(master, text="Otplata na rate", width=15,
command=self.otplata_na_rate_deiconify)
self.otplata_na_rate_button.place(x=95, y=155)
# TopLevel window
self.onr_top = Toplevel()
self.onr_top.title("Kalkulator troška")
self.onr_top.withdraw()
def otplata_na_rate_deiconify(self):
# Otplata_na_rate TopLevel:
self.master.withdraw()
self.onr_top.deiconify()
self.onr_top.protocol("WM_DELETE_WINDOW", lambda: (self.master.deiconify(), self.onr_top.withdraw()))
# Pull data from db and creating dynamic buttons (depends how many items are in db)
c.execute('SELECT Opis FROM OTPLATA_NA_RATE')
lista_opisa_rata = list()
for opis in c:
lista_opisa_rata.append(opis[0])
kolona = 0
red = 0
for opis in lista_opisa_rata:
rata_button = Button(self.onr_top, text=opis, width=10)
rata_button.grid(row=red, column=kolona, pady=5, padx=5)
kolona += 1
if kolona == 3:
red += 1
kolona = 0
w = self.onr_top.winfo_width()
h = self.onr_top.winfo_height()
x = self.master.winfo_x()
y = self.master.winfo_y()
print(w, h)
self.onr_top.geometry("%dx%d+%d+%d" % (w, h, x, y))
root = Tk()
my_gui = FinancialCalc(root)
root.mainloop()
Upvotes: 0
Views: 433
Reputation: 7710
In your code you called .winfo_width()
just after creating new widgets in the window. That sends events to expand the window but they aren't shown until tkinter
has had time to update.
In your code you could have added self.onr_top.update()
just before the self.onr_top.winfo_width()
and it might have worked.
Another solution would have been to use winfo_reqwidth()
instead of winfo_width()
.
But the easiest solution is to use:
self.onr_top.geometry(f"+{x}+{y}")
When using .geometry
, you can specify the widthxheight
or widthxheight+x+y
or just +x+y
Upvotes: 1