如何在 Python tkinter 中使用 Stringvar 收集字符串?
How to collect a string with Stringvar inside Python tkinter?
我正在尝试获取输入的信息,例如成绩和备注。对于年级,我使用的是 OptionMenu 并且打印完美,但我的笔记 (biljeska) 不会打印任何内容
代码示例:
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()
在 tkinter 中,mainloop 方法很复杂,我很少使用你的代码,但输出不是预期的,但是,很少转换到框架方法它工作良好,如果有用的话可以通过很少修改的代码
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()
刚刚为 windows 创建了框架并且循环被分配给主 window 作为 root...
我正在尝试获取输入的信息,例如成绩和备注。对于年级,我使用的是 OptionMenu 并且打印完美,但我的笔记 (biljeska) 不会打印任何内容
代码示例:
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()
在 tkinter 中,mainloop 方法很复杂,我很少使用你的代码,但输出不是预期的,但是,很少转换到框架方法它工作良好,如果有用的话可以通过很少修改的代码
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()
刚刚为 windows 创建了框架并且循环被分配给主 window 作为 root...