Home > Mobile >  How to collect a string with Stringvar inside Python tkinter?
How to collect a string with Stringvar inside Python tkinter?

Time:06-04

I'm trying to get an entered information such as grade and a note. For grade I'm using OptionMenu and it prints perfectly, but my note (biljeska) won't print whatsoever

Code example:

import tkinter as tk
import datetime
dt = datetime.datetime.today()
class OdabirRazreda():
    def __init__(self):
        self.window2 = tk.Tk()
        g=tk.Button(self.window2,command=self.unos)
        g.pack()

        self.ocjena = tk.StringVar()
        self.razred=tk.StringVar()
        self.biljeska=tk.StringVar()
        self.ucenik=tk.StringVar()
        self.window2.mainloop()

    def unos(self):
        self.window2.withdraw()
        self.window4 = tk.Tk()

        lbl_ocjena = tk.Label(self.window4, text="Ocjena")
        self.lbl_biljeska = tk.Label(self.window4, text="Bilješka:")


        lbl_ocjena.grid(row=0, column=0, sticky=tk.W, pady=2)
        self.lbl_biljeska.grid(row=1, column=0, sticky=tk.W, pady=2)


        ent_ocjena = tk.OptionMenu(self.window4, self.ocjena, "1", "2", "3","4","5")
        ent_ocjena.grid(row=0, column=1, pady=2, )

        ent_biljeska = tk.Entry(master=self.window4,textvariable=self.biljeska)
        ent_biljeska.grid(row=1, column=1, pady=2)
        btn_unesi = tk.Button(self.window4, text='Unesi',command=self.ispis)
        btn_unesi.grid(row=3, column=1,pady=2)
        self.window4.mainloop()
    def ispis(self):
        print(dt.day,dt.month,self.ocjena.get(),self.biljeska.get())
OdabirRazreda()

CodePudding user response:

In tkinter there is complication with mainloop method, i worked with your code little but outputs are not getting expected but, little convertion to frame methods its working good, if usefull can go throught the code of little modification

import tkinter as tk
import datetime

dt = datetime.datetime.today()


class OdabirRazreda():
    def __init__(self):
        self.root = tk.Tk()

        self.window2 = tk.Frame(self.root)
        self.window2.pack()

        g = tk.Button(self.window2, command=self.unos,)
        g.pack()

        self.ocjena = tk.StringVar()
        self.razred = tk.StringVar()
        self.biljeska = tk.StringVar()
        self.ucenik = tk.StringVar()
        self.root.mainloop()

    def unos(self):
        self.window2.pack_forget() # 'can call destroy() also if no use in further'
        self.window4 = tk.Frame()
        self.window4.pack()

        lbl_ocjena = tk.Label(self.window4, text="Ocjena")
        self.lbl_biljeska = tk.Label(self.window4, text="Bilješka:")

        lbl_ocjena.grid(row=0, column=0, sticky=tk.W, pady=2)
        self.lbl_biljeska.grid(row=1, column=0, sticky=tk.W, pady=2)

        ent_ocjena = tk.OptionMenu(self.window4, self.ocjena, "1", "2", "3", "4", "5")
        ent_ocjena.grid(row=0, column=1, pady=2, )

        ent_biljeska = tk.Entry(master=self.window4, textvariable=self.biljeska)
        ent_biljeska.grid(row=1, column=1, pady=2)

        btn_unesi = tk.Button(self.window4, text='Unesi', command=self.ispis)
        btn_unesi.grid(row=3, column=1, pady=2)

    def ispis(self):
        print(dt.day, dt.month, self.ocjena.get(), self.biljeska.get())


OdabirRazreda()

just created the frames for both the windows and looping is assigned to main window as root...

  • Related