Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法从GUI接收输入_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 无法从GUI接收输入

Python 无法从GUI接收输入,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我基本上是在做一个程序来计算某个形状的周长和面积。我正在使用tkinter GUI来实现这一点 无论如何,我决定让用户在输入完号码后突出显示checkbutton,以便我知道何时从tkinter中的entry小部件收集数据,但我的checkbutton似乎不起作用 def perTriangle(): #PerTriangle means the perimeter of a triangle. tk.Label(window, text = "Enter in the Nu

我基本上是在做一个程序来计算某个形状的周长和面积。我正在使用tkinter GUI来实现这一点

无论如何,我决定让用户在输入完号码后突出显示checkbutton,以便我知道何时从tkinter中的entry小部件收集数据,但我的checkbutton似乎不起作用

def perTriangle(): #PerTriangle means the perimeter of a triangle. 
    tk.Label(window, text = "Enter in the Numbers: ", background = "white").place(x = 1115, y = 100)
    tk.Label(window, text = "Side 1: ", background = "white").place(x = 1115, y = 130)
    entry1 = tk.Entry(window)
    entry1.place(x = 1155, y = 130)
    tk.Label(window, text = "Side 2: ", background = "white").place(x = 1115, y = 160)
    entry2 = tk.Entry(window)
    entry2.place(x = 1155, y = 160)
    tk.Label(window, text = "Side 3: ", background = "white").place(x = 1115, y = 190)
    entry3 = tk.Entry(window)
    entry3.place(x = 1155, y = 190)
    check1 = tk.IntVar()
    cButton = tk.Checkbutton(window, text = "Enter: ", variable = check1)
    cButton.place(x = 1115, y = 220)
    if check1.get() == 1:
        num1 = float(entry1.get())
        num2 = float(entry2.get())
        num3 = float(entry3.get())
        ans = num1 + num2 + num3
        tk.messagebox.showinfo("Your Answer is: ", str(ans))
选中“checkbutton”后将不运行任何内容。我不知道我做错了什么,因为我认为一切都正常,但仍然不起作用。我没有得到这个程序的任何语法错误

然后,我尝试使用多个函数,看看这是否能奏效:

def perTriangle():
    tk.Label(window, text = "Enter in the Numbers: ", background = "white").place(x = 1115, y = 100)
    tk.Label(window, text = "Side 1: ", background = "white").place(x = 1115, y = 130)
    entry1 = tk.Entry(window)
    entry1.place(x = 1155, y = 130)
    tk.Label(window, text = "Side 2: ", background = "white").place(x = 1115, y = 160)
    entry2 = tk.Entry(window)
    entry2.place(x = 1155, y = 160)
    tk.Label(window, text = "Side 3: ", background = "white").place(x = 1115, y = 190)
    entry3 = tk.Entry(window)
    entry3.place(x = 1155, y = 190)
    check1 = tk.IntVar()
    cButton = tk.Checkbutton(window, text = "Enter: ", variable = check1)
    cButton.place(x = 1115, y = 220)
    isUserDone(check1)


def isUserDone(x):
    if x.get() == 1:
        calculationPTriangle()


def calculationPTriangle():
    num1 = float(entry1.get())
    num2 = float(entry2.get())
    num3 = float(entry3.get())
    ans = num1 + num2 + num3
    tk.messagebox.showinfo("Your Answer is: ", str(ans))
再一次,我得到了同样的结果。我能做些什么来解决这个问题?我感谢所有的帮助!提前谢谢

初始编辑后更新的代码:

def perTriangle():
    tk.Label(window, text = "Enter in the Numbers: ", background = "white").place(x = 1115, y = 100)
    tk.Label(window, text = "Side 1: ", background = "white").place(x = 1115, y = 130)
    entry1 = tk.Entry(window)
    entry1.place(x = 1155, y = 130)
    tk.Label(window, text = "Side 2: ", background = "white").place(x = 1115, y = 160)
    entry2 = tk.Entry(window)
    entry2.place(x = 1155, y = 160)
    tk.Label(window, text = "Side 3: ", background = "white").place(x = 1115, y = 190)
    entry3 = tk.Entry(window)
    entry3.place(x = 1155, y = 190)
    check1 = tk.IntVar()
    cButton = tk.Checkbutton(window, text = "Enter: ", variable = check1, command = calculationPTriangle)
    cButton.place(x = 1115, y = 220)

def calculationPTriangle():
    num1 = float(entry1.get())
    num2 = float(entry2.get())
    num3 = float(entry3.get())
    ans = num1 + num2 + num3
    tk.messagebox.showinfo("Your Answer is: ", str(ans))

您的问题是
GUI
不像
input()
那样工作,小部件不会等待您的数据,您甚至在
tkinter
显示窗口之前检查小部件中的值。或者在您有机会将数据放入小部件之前

通常我们在将代码放入小部件后使用
按钮运行代码,然后它就有机会获得这些值

按钮
具有
command=function\u name
来分配当您单击按钮时将运行的功能

顺便说一句:
命令=
需要不带
()

Checkbutton
还有
command=
在您更改选择时运行该功能。但它将在您选择它时执行,也将在您取消选择它时执行

import tkinter as tk
        
# --- functions ---

def on_change():
    print('Entry:', entry.get())
    print('Checkbox:', checkbox_var.get())

# --- main ---

root = tk.Tk()

label = tk.Label(root, text='Input')
label.pack()

entry = tk.Entry(root)
entry.pack()

checkbox_var = tk.IntVar()
checkbox = tk.Checkbutton(root, text="Enter: ", variable=checkbox_var, command=on_change)
checkbox.pack()

root.mainloop()   

编辑:带有
全局的版本

import tkinter as tk
        
# --- functions ---

def on_change():
    print('Entry:', entry.get())
    print('Checkbox:', checkbox_var.get())

def main():
    #global root   # if I would need `root` in other function
    #global label  # if I would need `label` in other function
    global entry
    global checkbox_var
    
    root = tk.Tk()
    
    label = tk.Label(root, text='Input')
    label.pack()
    
    entry = tk.Entry(root)
    entry.pack()
    
    checkbox_var = tk.IntVar()
    checkbox = tk.Checkbutton(root, text="Enter: ", variable=checkbox_var, command=on_change)
    checkbox.pack()
    
    root.mainloop()
    
# --- main ---

main()
    

您的问题是
GUI
不像
input()
那样工作,小部件不会等待您的数据,您甚至在
tkinter
显示窗口之前检查小部件中的值。或者在您有机会将数据放入小部件之前

通常我们在将代码放入小部件后使用
按钮运行代码,然后它就有机会获得这些值

按钮
具有
command=function\u name
来分配当您单击按钮时将运行的功能

顺便说一句:
命令=
需要不带
()

Checkbutton
还有
command=
在您更改选择时运行该功能。但它将在您选择它时执行,也将在您取消选择它时执行

import tkinter as tk
        
# --- functions ---

def on_change():
    print('Entry:', entry.get())
    print('Checkbox:', checkbox_var.get())

# --- main ---

root = tk.Tk()

label = tk.Label(root, text='Input')
label.pack()

entry = tk.Entry(root)
entry.pack()

checkbox_var = tk.IntVar()
checkbox = tk.Checkbutton(root, text="Enter: ", variable=checkbox_var, command=on_change)
checkbox.pack()

root.mainloop()   

编辑:带有
全局的版本

import tkinter as tk
        
# --- functions ---

def on_change():
    print('Entry:', entry.get())
    print('Checkbox:', checkbox_var.get())

def main():
    #global root   # if I would need `root` in other function
    #global label  # if I would need `label` in other function
    global entry
    global checkbox_var
    
    root = tk.Tk()
    
    label = tk.Label(root, text='Input')
    label.pack()
    
    entry = tk.Entry(root)
    entry.pack()
    
    checkbox_var = tk.IntVar()
    checkbox = tk.Checkbutton(root, text="Enter: ", variable=checkbox_var, command=on_change)
    checkbox.pack()
    
    root.mainloop()
    
# --- main ---

main()
    

GUI
不像
input()
那样工作-小部件不等待值,您甚至在
tkinter
显示窗口之前检查widgest中的值。通常我们使用
按钮
运行代码,在我们将值放入小部件后从小部件中获取值。
GUI
不像
input()
-小部件不等待值,甚至在
tkinter
显示窗口之前,您都会在widgest中检查值。通常我们使用
按钮
来运行代码,在我们将值放入小部件后从小部件中获取值。我尝试使用您的建议,我的代码现在在我的原始帖子中更新。当我试图获取
entry1
entry2
entry3
中的值时,我得到一个错误:
AttributeError:“str”对象没有属性“get”。
如何使用它?似乎您将字符串分配给了
entry1
,而不是
Entry
。例如,如果在函数内部创建变量时执行
entry1=entry1.get()
,则不能再次执行
entry1.get()
,那么它是局部变量,不会更改外部/全局变量-因此,如果在函数外部创建
entry1=“”
,然后执行
entry1=tk.Entry()
内部函数,则不会更改
entry1=“”
。您必须在函数内部添加
global entry1
,通知函数必须将
Entry()
赋值给外部变量,而不是创建局部变量。在回答中看到新代码我尝试使用你的建议,我的代码现在在我原来的帖子中更新了。当我试图获取
entry1
entry2
entry3
中的值时,我得到一个错误:
AttributeError:“str”对象没有属性“get”。
如何使用它?似乎您将字符串分配给了
entry1
,而不是
Entry
。例如,如果在函数内部创建变量时执行
entry1=entry1.get()
,则不能再次执行
entry1.get()
,那么它是局部变量,不会更改外部/全局变量-因此,如果在函数外部创建
entry1=“”
,然后执行
entry1=tk.Entry()
内部函数,则不会更改
entry1=“”
。您必须在函数内部添加
global entry1
,通知函数必须将
Entry()
赋值给外部变量,而不是创建局部变量。请参见答案中的新代码