我想循环编写以下代码。不幸的是,我的循环根本不起作用。谁能帮助我将StringVar,标签和输入框放入for或while循环中?
airp_elev = tkinter.StringVar()
QNH = tkinter.StringVar()
OAT = tkinter.StringVar()
L0 = tkinter.Label(root, text = "Welcome to the density altutude calculator!", fg = "red").grid(row = 0, column = 1, sticky = "W", padx = 10, pady = 10)
L1 = tkinter.Label(root, text = "Airport elevation [ft]").grid(row = 1, column = 0, sticky = "W", padx = 10, pady = 10)
L2 = tkinter.Label(root, text = "QNH [hPa]").grid(row = 2, column = 0, sticky = "W", padx = 10, pady = 10)
L3 = tkinter.Label(root, text = "Ourside air temperature [°C]").grid(row = 3, column = 0, sticky = "W", padx = 10, pady = 10)
E1 = tkinter.Entry(root, textvariable = airp_elev, bd = 5).grid(row = 1, column = 1)
E2 = tkinter.Entry(root, textvariable = QNH, bd = 5).grid(row = 2, column = 1)
E3 = tkinter.Entry(root, textvariable = OAT, bd = 5).grid(row = 3, column = 1)
答案 0 :(得分:1)
这是一个方便的函数,称为enumerate
,当您要grid
一系列小部件时,该函数特别有用。您可以阅读here。
由于您将标签和条目并排对齐,因此实际上可以通过在for循环中一起使用zip
和enumerate
来一次性完成这两个操作:
import tkinter
root = tkinter.Tk()
airp_elev = tkinter.StringVar()
QNH = tkinter.StringVar()
OAT = tkinter.StringVar()
all_labels = ("Airport elevation [ft]","QNH [hPa]","Ourside air temperature [°C]") #group your labels together
all_var = (airp_elev,QNH,OAT) #group your vars together
L0 = tkinter.Label(root, text = "Welcome to the density altutude calculator!", fg = "red").grid(row = 0, column = 1, sticky = "W", padx = 10, pady = 10)
for num,i in enumerate(zip(all_labels,all_var),1): #enumerate through both lists
label, entry = i #unpack the tuple i into two easy to read variables
tkinter.Label(root,text=label).grid(row=num,column=0,sticky = "W", padx = 10, pady = 10)
tkinter.Entry(root, textvariable=entry, bd=5).grid(row=num, column=1)
tkinter.Button(root,text="Print result",command=lambda: print(airp_elev.get(),QNH.get(),OAT.get())).grid(row=4,column=0,columnspan=2)
root.mainloop()
答案 1 :(得分:0)
这是一个小例子。您的问题可能与需要从小部件创建中分离.grid(或打包任何内容)方法有关,否则您将丢失对小部件的引用。
import tkinter as tk
from tkinter import Tk
def print_myvars():
for key in myvars:
print(myvars[key].get())
root = Tk()
fields = ['One', 'Two', 'Three']
myvars = dict()
widgets = dict()
for field in fields:
myvars[field] = tk.StringVar()
widgets[field] = tk.Entry(
root, text=field, textvariable=myvars[field])
widgets[field].pack()
print_myvars_button = tk.Button(
root, text='Print Vars', command=print_myvars)
print_myvars_button.pack()
root.mainloop()
答案 2 :(得分:0)
我想向您展示我的代码。这是一个密度高度计算器,密度高度计算主要用于航空领域。
import tkinter
root = tkinter.Tk()
root.title("Density altitude calculator")
root.geometry("500x275")
airp_elev = tkinter.StringVar()
QNH = tkinter.StringVar()
OAT = tkinter.StringVar()
all_labels = ("Airport elevation [ft]","QNH [hPa]","Ourside air temperature [°C]") #group your labels together
all_var = (airp_elev,QNH,OAT) #group your vars together
L0 = tkinter.Label(root, text = "Welcome to the density altutude calculator!", fg = "red").grid(row = 0, column = 1, sticky = "W", padx = 10, pady = 10)
def calculateda():
ad_elev = float(airp_elev.get())
pres = float(QNH.get())
temp = float(OAT.get())
QNE = 1013
isa_st = 15
ad = ((QNE - pres) * 28)
pa = ad_elev + ad
st_pa = isa_st - (pa / 1000 * 2)
hd = 120 * (temp - st_pa)
da = round(pa + hd)
labelresult = tkinter.Label(root, text = "The density altitude is: %.d ft" % da).grid(row = 7, column = 1, padx = 10, pady = 10)
return
for num,i in enumerate(zip(all_labels, all_var),1): #enumerate through both lists
label, entry = i #unpack the tuple i into two easy to read variables
tkinter.Label(root, text = label).grid(row = num, column =0 , sticky = "W", padx = 10, pady = 10)
tkinter.Entry(root, textvariable = entry, bd = 5).grid(row = num, column = 1)
b1 = tkinter.Button(root, text = "Calculate density altitude", command = calculateda).grid(row = 4, column = 1, padx = 10, pady = 10)
root.mainloop()
如果您有任何想法如何改进此代码,请回答。欢迎任何评论。
亲切的问候 凯文