所以我想制作一个可以保存入口点的代码,我已经做到了,但是出现了一个问题所以这是我的代码:
import tkinter as tk
def players():
global p1, p2
p1 = plyr1.get()
p2 = plyr2.get()
#USERNAMES
if (p1 != "") or (p2 != "") or (p1 != p2):
print("Player 1 = " + p1)
print("Player 2 = " + p2)
if (p1 == "") or (p2 == ""):
frame1.tkraise()
messagebox.showerror("INVALID USRNAME", "PLEASE FILL THE ENTRY POINT !")
elif p1 == p2:
print(p1 + " (Player 1's username)"+ " and " + p2 + " (Player 2's username)"+ " ARE SAME")
frame1.tkraise()
messagebox.showerror("INVALID USERNAME", "PLEASE USE DIFFERENT USERNAME !")
else :
frame2.tkraise()
root = tk.Tk()
.
.
.
tk.Label(frame3, text = "Player 2 = ", font = _font).grid(row = 2, column = 0, sticky = 'W')
**p2_data = tk.Label(frame3, text = p2, font = _font )**
p2_data.grid(row = 2, column = 1, sticky = 'E')
因此,在该粗体行中,它表示 p2 未定义。任何想法为什么以及如何解决它?
问题是您没有正确返回变量。在您的函数中,您将变量设为全局变量。通常的做法是限制全局变量的使用,而是将变量从函数返回到代码中,即return your_variable
获取返回的变量并将其存储,在其余代码中,编写your_variable = your_function()
函数中的变量是局部的。它们需要返回到主代码。
例如:
def your_function():
widget_input = entry_widget.get()
# returns your variable to the rest of the code
return widget_input
# stores the output of the function in a variable
your_var = your_function()
由于您有两个变量,因此您需要返回它们并保存它们:
def your_function():
input1 = widget1.get()
input2 = widget2.get()
return input1, input2
va1, var2 = your_function()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句