问题描述
限时送ChatGPT账号..这是一个总体模型,因此条目必须是整数才能用于计算.
This is a population model so the entries must be integers to be used for calculation.
import sys
import tkinter
from tkinter import*
import time
global v
global popJ
popJ = 0
def genInput(): #Allows the user to input the data
gen = Tk()
gen.wm_title("Data Input")
v = IntVar()
ent1 = Entry(gen, textvariable = v).pack()
ent1Txt = Label(gen, text = 'Input Juvenile Populations')
ent1Txt.pack()
v2 = StringVar()
ent2 = Entry(gen, textvariable = v2)
ent2Txt = Label(gen, text = 'Input Adult Populations')
ent2.pack()
ent2Txt.pack()
v3 = StringVar()
ent3 = Entry(gen, textvariable = v3)
ent3Txt = Label(gen, text = 'Input Senile Populations')
ent3.pack()
ent3Txt.pack()
v4 = StringVar()
ent4 = Entry(gen, textvariable = v4)
ent4Txt = Label(gen, text = 'Input Survival rates for Juveniles')
ent4.pack()
ent4Txt.pack()
v5 = StringVar()
ent5 = Entry(gen, textvariable = v5)
ent5Txt = Label(gen, text = 'Input Survival rates for Adults')
ent5.pack()
ent5Txt.pack()
v6 = StringVar()
ent6 = Entry(gen, textvariable = v6)
ent6Txt = Label(gen, text = 'Input Survival rates for Seniles')
ent6.pack()
ent6Txt.pack()
v7 = StringVar()
ent7 = Entry(gen, textvariable = v7)
ent7Txt = Label(gen, text = 'Input the birth rate')
ent7.pack()
ent7Txt.pack()
v8 = StringVar()
ent8 = Entry(gen, textvariable = v8)
ent8Txt = Label(gen, text = 'Number of Generations')
ent8.pack()
ent8Txt.pack()
def quit1(): # Need to be here or it breaks the program
gen.destroy()
return
def submit():
popJ = v.get()
popJtxt = Label(gen, text= v.get()).pack()
return
submit1= Button(gen, text="Submit")
submit1.pack()
submit1.configure(command = submit)
return1 = Button(gen, text = 'Return to Menu')
return1.pack(pady=30)
return1.configure(command = quit1)
return
def genView(): # should display the data
disp = Tk()
disp.wm_title('Displaying data Values')
popJuvenilesTxt = Label (disp, text = popJ)
popJuvenilesTxt.grid(row =1, column = 1)
def menu(): # creates the gui menu
menu = Tk()
menu.wm_title("Greenfly model")
genInp = Button(menu,text = "Set Generation Values")
genVew = Button(menu,text = 'Dysplay Generation Values')
modelCal = Button(menu,text = 'Run model')
exportData = Button(menu,text = 'Export Data')
genTxt = Label(menu, text='Input the Generation values')
genvTxt = Label (menu, text = 'View the current generation values')
modelTxt = Label (menu, text = 'Run the model')
exportTxt = Label (menu, text = 'Export data')
genInp.grid(row=1, column=1)
genVew.grid(row=2, column=1)
modelCal.grid(row=3, column=1)
exportData.grid(row=4 , column=1)
genTxt.grid(row=1, column = 2)
genvTxt.grid(row=2, column =2)
modelTxt.grid(row=3, column =2)
exportTxt.grid(row=4, column = 2)
genInp.configure(command = genInput)
genVew.configure(command = genView)
menu()
即使使用了 .get(),'Submit' 和 genView 部分仍然返回 0.如果使用 StringVar,它将返回一个空格.运行模型和导出数据功能尚未实现.
The 'Submit' and the genView section keep returning 0 even though .get() is used. If StringVar is used it returns a blank space. The run model and export data functions are not yet implemented.
推荐答案
问题是您正在创建多个 Tk
实例,对于子窗口,您应该使用 Toplevel
相反,所以这有效:
the issue is that you are creating multiple Tk
instances, for child windows you should be using Toplevel
instead, so this works:
import sys
import tkinter
from tkinter import *
import time
global v
global popJ
popJ = 0
def genInput(): #Allows the user to input the data
gen = Toplevel()
gen.wm_title("Data Input")
v = IntVar()
ent1 = Entry(gen, textvariable = v).pack()
ent1Txt = Label(gen, text = 'Input Juvenile Populations')
ent1Txt.pack()
v2 = StringVar()
ent2 = Entry(gen, textvariable = v2)
ent2Txt = Label(gen, text = 'Input Adult Populations')
ent2.pack()
ent2Txt.pack()
v3 = StringVar()
ent3 = Entry(gen, textvariable = v3)
ent3Txt = Label(gen, text = 'Input Senile Populations')
ent3.pack()
ent3Txt.pack()
v4 = StringVar()
ent4 = Entry(gen, textvariable = v4)
ent4Txt = Label(gen, text = 'Input Survival rates for Juveniles')
ent4.pack()
ent4Txt.pack()
v5 = StringVar()
ent5 = Entry(gen, textvariable = v5)
ent5Txt = Label(gen, text = 'Input Survival rates for Adults')
ent5.pack()
ent5Txt.pack()
v6 = StringVar()
ent6 = Entry(gen, textvariable = v6)
ent6Txt = Label(gen, text = 'Input Survival rates for Seniles')
ent6.pack()
ent6Txt.pack()
v7 = StringVar()
ent7 = Entry(gen, textvariable = v7)
ent7Txt = Label(gen, text = 'Input the birth rate')
ent7.pack()
ent7Txt.pack()
v8 = StringVar()
ent8 = Entry(gen, textvariable = v8)
ent8Txt = Label(gen, text = 'Number of Generations')
ent8.pack()
ent8Txt.pack()
def quit1(): # Need to be here or it breaks the program
gen.destroy()
return
def submit():
popJ = v.get()
popJtxt = Label(gen, text= popJ).pack() # as stated in comments, this line will return none to popJtxt, so it pointless to assign it
return
submit1= Button(gen, text="Submit")
submit1.pack()
submit1.configure(command = submit)
return1 = Button(gen, text = 'Return to Menu')
return1.pack(pady=30)
return1.configure(command = quit1)
return
def genView(): # should display the data
disp = Toplevel()
disp.wm_title('Displaying data Values')
popJuvenilesTxt = Label (disp, text = popJ)
popJuvenilesTxt.grid(row =1, column = 1)
def menu(): # creates the gui menu
menu = Tk()
menu.wm_title("Greenfly model")
genInp = Button(menu,text = "Set Generation Values")
genVew = Button(menu,text = 'Dysplay Generation Values')
modelCal = Button(menu,text = 'Run model')
exportData = Button(menu,text = 'Export Data')
genTxt = Label(menu, text='Input the Generation values')
genvTxt = Label (menu, text = 'View the current generation values')
modelTxt = Label (menu, text = 'Run the model')
exportTxt = Label (menu, text = 'Export data')
genInp.grid(row=1, column=1)
genVew.grid(row=2, column=1)
modelCal.grid(row=3, column=1)
exportData.grid(row=4 , column=1)
genTxt.grid(row=1, column = 2)
genvTxt.grid(row=2, column =2)
modelTxt.grid(row=3, column =2)
exportTxt.grid(row=4, column = 2)
genInp.configure(command = genInput)
genVew.configure(command = genView)
menu()
要解释为什么会发生这种情况,您需要了解所有 tk 变量都是使用关联的 Tk 实例(通常是第一个实例)创建的,因此将属于一个 tk 实例的变量绑定到在另一个实例中创建的显示将永远不会更新该变量,创建一个 Toplevel
而不是 Tk
将它与第一个 Tk
实例相关联
to explain why this happens, you need to understand that all tk variables are created with an associated Tk instance (usually the first instance) so binding a variable belonging to one tk instance to a display created in another will never update the variable, creating a Toplevel
instead of Tk
associates it with the first Tk
instance
这篇关于即使使用 .get() 函数,IntVar 也只返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论