使用.get()函数

时间:2016-01-26 09:47:23

标签: python tkinter tkinter-canvas

这是一个填充模型,因此条目必须是整数才能用于计算。

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,则返回空格。运行模型和导出数据功能尚未实现。

3 个答案:

答案 0 :(得分:2)

问题是您要创建多个Tk个实例,对于您应该使用Toplevel的子窗口,这样可行:

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实例的变量绑定到另一个tk实例中创建的显示将永远不会更新变量,创建Toplevel而不是Tk将其与第一个Tk实例相关联

答案 1 :(得分:2)

您不应使用Tk()创建多个根/主窗口。将Tk()genInput()中的genView()来电更改为Toplevel()

同样menu()在函数末尾应该有一个menu.mainloop()

正如furas在评论中提到的那样,.pack().grid()方法返回None,而不是小部件,因此存储调用这些方法的结果毫无意义。

其他一些观察......

import time,但您似乎没有使用它。如果您打算在某处使用time.sleep(),请不要:它不会与Tkinter的事件循环合作;还有其他方法可以使用Tkinter提供的方法进行延迟。

您有import tkinterfrom tkinter import *import tkinter允许您通过执行例如tkinter.Entry来引用Tkinter实体,但您在代码中的任何位置都没有使用该语法。 from tkinter import *允许您执行Entry,但缺点是脚本的命名空间被Tkinter定义的所有名称破坏。更好的折衷方案是使用import tkinter as tk,然后你可以tk.Entry,这样就可以避免命名空间污染而只需要更多的输入。

如果你将它放入课堂,你的代码会更整洁。这样你就不需要使用global了。但是,我应该注意到,您实际上并没有在代码中正确使用global。您使用它来“声明”全局范围中的变量,而是应该进入函数(或方法)定义以指示该函数正在修改全局范围中的名称。

这是您的程序的“精简”版本,可以正确设置和获取popJ值。此代码在Python 2.6.6上进行了测试,要在Python 3上运行,您需要将import语句更改为from tkinter import *

from Tkinter import *

popJ = 0

def genInput(): #Allows the user to input the data
    gen = Toplevel()
    gen.wm_title("Data Input")
    v = IntVar()
    v.set(popJ)
    ent1 = Entry(gen, textvariable = v)
    ent1.pack()
    Label(gen, text = 'Input Juvenile Populations').pack()

    def quit1():   # Need to be here or it breaks the program
        gen.destroy()

    def submit():
        global popJ
        popJ = v.get()
        Label(gen, text=popJ).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)

def menu():  # creates the gui menu
    menu = Tk()
    menu.wm_title("Greenfly model")
    genInp = Button(menu,text = "Set Generation Values")
    genInp.grid(row=1, column=1)
    genInp.configure(command = genInput)
    menu.mainloop()

menu()

答案 2 :(得分:0)

您必须在global popJ中使用submit

现在在popJ

中创建局部变量submit

编辑:也许global不是首选方法,但它适用于您的示例

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():
        global popJ

        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 = 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.mainloop()

menu()

当然您也需要menu.mainloop()Toplevel代替Tk