Python 3.X - StringVar 总是空的!印刷 ''
Python 3.X - StringVar always empty! Prints ''
from tkinter import *
def Game(result):
InputScreen = Tk()
InputScreen.configure(background = "light blue")
InputScreen.geometry("800x400+100+50")
InputScreen.title("Input Screen")
gold_e = StringVar()
textentry2 = Entry(InputScreen,
textvariable = gold_e, width = 5).place(x = 325, y = 80)
button3 = Button(InputScreen,
command = lambda: (Update(gold_e)),
text = "Update",
fg = "black",
bg = "red").place(x = 300, y = 300)
def Update(values):
print("qqw"+str(values.get())+"wer")
gold_v = values.get()
print(gold_v)
Label19 = Label(InputScreen, text = gold_v,
fg = "black",
bg = "light blue").place(x = 390,y = 80)
Game(0)
此代码绝对可以正常工作:但是,当它被以下代码包围时,它总是 returns ''(空白),我不知道为什么。
import sys
from tkinter import *
def Game(result):
InputScreen = Tk()
InputScreen.configure(background = "light blue")
InputScreen.geometry("800x400+100+50")
InputScreen.title("Input Screen")
goldpps = "0.5"
silverpps = "0.8"
copperpps = "1.2"
oilpps = "3.4"
wheatpps = "0.6"
dieselpps = "0.8"
cocopps = "3.0"
Label0 = Label(InputScreen, text = "£" + str(result),
fg = "black",
bg = "light blue").place(x = 100,y = 20)
Label1 = Label(InputScreen, text = "Commodities",
fg = "black",
bg = "light blue").place(x = 100,y = 50)
Label2 = Label(InputScreen, text = "Gold",
fg = "black",
bg = "light blue").place(x = 125,y = 80)
Label3 = Label(InputScreen, text = "Silver",
fg = "black",
bg = "light blue").place(x = 124,y = 110)
Label4 = Label(InputScreen, text = "Copper",
fg = "black",
bg = "light blue").place(x = 119,y = 140)
Label5 = Label(InputScreen, text = "Oil",
fg = "black",
bg = "light blue").place(x = 130,y = 170)
Label6 = Label(InputScreen, text = "Wheat",
fg = "black",
bg = "light blue").place(x = 120,y = 200)
Label7 = Label(InputScreen, text = "Diesel",
fg = "black",
bg = "light blue").place(x = 121,y = 230)
Label8 = Label(InputScreen, text = "Coco",
fg = "black",
bg = "light blue").place(x = 122,y = 260)
Label9 = Label(InputScreen, text = "Price per Share",
fg = "black",
bg = "light blue").place(x = 200,y = 50)
Label10 = Label(InputScreen, text = "Quantity",
fg = "black",
bg = "light blue").place(x = 315,y = 50)
Label11 = Label(InputScreen, text = "Cost",
fg = "black",
bg = "light blue").place(x = 390,y = 50)
Label12 = Label(InputScreen, text = goldpps,
fg = "black",
bg = "light blue").place(x = 227,y = 80)
Label13 = Label(InputScreen, text = silverpps,
fg = "black",
bg = "light blue").place(x = 227,y = 110)
Label14 = Label(InputScreen, text = copperpps,
fg = "black",
bg = "light blue").place(x = 227,y = 140)
Label15 = Label(InputScreen, text = oilpps,
fg = "black",
bg = "light blue").place(x = 227,y = 170)
Label16 = Label(InputScreen, text = wheatpps,
fg = "black",
bg = "light blue").place(x = 227,y = 200)
Label17 = Label(InputScreen, text = dieselpps,
fg = "black",
bg = "light blue").place(x = 227,y = 230)
Label18 = Label(InputScreen, text = cocopps,
fg = "black",
bg = "light blue").place(x = 227,y = 260)
silver_e = IntVar()
copper_e = IntVar()
oil_e = IntVar()
wheat_e = IntVar()
diesel_e = IntVar()
coco_e = IntVar()
gold_e = StringVar()
textentry2 = Entry(InputScreen,
textvariable = gold_e, width = 5).place(x = 325, y = 80)
textentry3 = Entry(InputScreen,
textvariable = silver_e, width = 5).place(x = 325, y = 110)
textentry4 = Entry(InputScreen,
textvariable = copper_e, width = 5).place(x = 325, y = 140)
textentry5 = Entry(InputScreen,
textvariable = oil_e, width = 5).place(x = 325, y = 170)
textentry6 = Entry(InputScreen,
textvariable = wheat_e, width = 5).place(x = 325, y = 200)
textentry7 = Entry(InputScreen,
textvariable = diesel_e, width = 5).place(x = 325, y = 230)
textentry8 = Entry(InputScreen,
textvariable = coco_e, width = 5).place(x = 325, y = 260)
button3 = Button(InputScreen,
command = lambda: (Update(gold_e)),
text = "Update",
fg = "black",
bg = "red").place(x = 300, y = 300)
def Update(values):
print("qqw"+str(values.get())+"wer")
gold_v = values.get()
print(gold_v)
Label19 = Label(InputScreen, text = gold_v,
fg = "black",
bg = "light blue").place(x = 390,y = 80)
print(values.get())
我和一个朋友花了 5 个多小时来检查这个,但无法发现错误:如果你说用 类 重写它,那不是一个选项。代码的想法是避免编写我们自己的 类。
代码本身运行良好(在主代码块底部添加 Game(0)),直到您从另一个文件调用此文件,将 'value' 传递到 Game 函数中。
我的猜测是您正在创建多个根 window,因为您说您是从另一个文件传入 "value",而这个 "value" 是一个实例一个 tkinter 变量。由于您不能在不创建根 window 的情况下创建这些变量之一,因此其他文件必须创建根 window.
你的整个 tkinter 程序应该只创建一个根 window。如果您需要创建多个 window,则需要创建 Toplevel
的实例。
from tkinter import *
def Game(result):
InputScreen = Tk()
InputScreen.configure(background = "light blue")
InputScreen.geometry("800x400+100+50")
InputScreen.title("Input Screen")
gold_e = StringVar()
textentry2 = Entry(InputScreen,
textvariable = gold_e, width = 5).place(x = 325, y = 80)
button3 = Button(InputScreen,
command = lambda: (Update(gold_e)),
text = "Update",
fg = "black",
bg = "red").place(x = 300, y = 300)
def Update(values):
print("qqw"+str(values.get())+"wer")
gold_v = values.get()
print(gold_v)
Label19 = Label(InputScreen, text = gold_v,
fg = "black",
bg = "light blue").place(x = 390,y = 80)
Game(0)
此代码绝对可以正常工作:但是,当它被以下代码包围时,它总是 returns ''(空白),我不知道为什么。
import sys
from tkinter import *
def Game(result):
InputScreen = Tk()
InputScreen.configure(background = "light blue")
InputScreen.geometry("800x400+100+50")
InputScreen.title("Input Screen")
goldpps = "0.5"
silverpps = "0.8"
copperpps = "1.2"
oilpps = "3.4"
wheatpps = "0.6"
dieselpps = "0.8"
cocopps = "3.0"
Label0 = Label(InputScreen, text = "£" + str(result),
fg = "black",
bg = "light blue").place(x = 100,y = 20)
Label1 = Label(InputScreen, text = "Commodities",
fg = "black",
bg = "light blue").place(x = 100,y = 50)
Label2 = Label(InputScreen, text = "Gold",
fg = "black",
bg = "light blue").place(x = 125,y = 80)
Label3 = Label(InputScreen, text = "Silver",
fg = "black",
bg = "light blue").place(x = 124,y = 110)
Label4 = Label(InputScreen, text = "Copper",
fg = "black",
bg = "light blue").place(x = 119,y = 140)
Label5 = Label(InputScreen, text = "Oil",
fg = "black",
bg = "light blue").place(x = 130,y = 170)
Label6 = Label(InputScreen, text = "Wheat",
fg = "black",
bg = "light blue").place(x = 120,y = 200)
Label7 = Label(InputScreen, text = "Diesel",
fg = "black",
bg = "light blue").place(x = 121,y = 230)
Label8 = Label(InputScreen, text = "Coco",
fg = "black",
bg = "light blue").place(x = 122,y = 260)
Label9 = Label(InputScreen, text = "Price per Share",
fg = "black",
bg = "light blue").place(x = 200,y = 50)
Label10 = Label(InputScreen, text = "Quantity",
fg = "black",
bg = "light blue").place(x = 315,y = 50)
Label11 = Label(InputScreen, text = "Cost",
fg = "black",
bg = "light blue").place(x = 390,y = 50)
Label12 = Label(InputScreen, text = goldpps,
fg = "black",
bg = "light blue").place(x = 227,y = 80)
Label13 = Label(InputScreen, text = silverpps,
fg = "black",
bg = "light blue").place(x = 227,y = 110)
Label14 = Label(InputScreen, text = copperpps,
fg = "black",
bg = "light blue").place(x = 227,y = 140)
Label15 = Label(InputScreen, text = oilpps,
fg = "black",
bg = "light blue").place(x = 227,y = 170)
Label16 = Label(InputScreen, text = wheatpps,
fg = "black",
bg = "light blue").place(x = 227,y = 200)
Label17 = Label(InputScreen, text = dieselpps,
fg = "black",
bg = "light blue").place(x = 227,y = 230)
Label18 = Label(InputScreen, text = cocopps,
fg = "black",
bg = "light blue").place(x = 227,y = 260)
silver_e = IntVar()
copper_e = IntVar()
oil_e = IntVar()
wheat_e = IntVar()
diesel_e = IntVar()
coco_e = IntVar()
gold_e = StringVar()
textentry2 = Entry(InputScreen,
textvariable = gold_e, width = 5).place(x = 325, y = 80)
textentry3 = Entry(InputScreen,
textvariable = silver_e, width = 5).place(x = 325, y = 110)
textentry4 = Entry(InputScreen,
textvariable = copper_e, width = 5).place(x = 325, y = 140)
textentry5 = Entry(InputScreen,
textvariable = oil_e, width = 5).place(x = 325, y = 170)
textentry6 = Entry(InputScreen,
textvariable = wheat_e, width = 5).place(x = 325, y = 200)
textentry7 = Entry(InputScreen,
textvariable = diesel_e, width = 5).place(x = 325, y = 230)
textentry8 = Entry(InputScreen,
textvariable = coco_e, width = 5).place(x = 325, y = 260)
button3 = Button(InputScreen,
command = lambda: (Update(gold_e)),
text = "Update",
fg = "black",
bg = "red").place(x = 300, y = 300)
def Update(values):
print("qqw"+str(values.get())+"wer")
gold_v = values.get()
print(gold_v)
Label19 = Label(InputScreen, text = gold_v,
fg = "black",
bg = "light blue").place(x = 390,y = 80)
print(values.get())
我和一个朋友花了 5 个多小时来检查这个,但无法发现错误:如果你说用 类 重写它,那不是一个选项。代码的想法是避免编写我们自己的 类。
代码本身运行良好(在主代码块底部添加 Game(0)),直到您从另一个文件调用此文件,将 'value' 传递到 Game 函数中。
我的猜测是您正在创建多个根 window,因为您说您是从另一个文件传入 "value",而这个 "value" 是一个实例一个 tkinter 变量。由于您不能在不创建根 window 的情况下创建这些变量之一,因此其他文件必须创建根 window.
你的整个 tkinter 程序应该只创建一个根 window。如果您需要创建多个 window,则需要创建 Toplevel
的实例。