我正在与我的Python Tkinter GUI进行斗争。我想更新PickedFileName的文本以更改为PickFileButton()中的文件名。在线结果表明,我可以使用StringVar()或.config()实现这一点,但这两个选项似乎都不配合。使用StringVar()仅将文本呈现为py_var0,而在运行代码时,使用.config()似乎无法识别PickedFileName(即未定义)。我还想在按下ParseFileButton后为“[filename]generated”创建一个类似的标签,其中[filename.log]将变成[filename.txt],但这也会看到类似的问题。其他一切似乎都是如此。 我在下面附上了一段相关代码
class App:
def __init__(self, root):
#setting title
root.title("TF2 Log Parser")
#setting window size
width=500
height=400
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
ParserTitle=tk.Message(root)
ParserTitle["font"] = 'Bahnschrift', 24
ParserTitle["fg"] = "#333333"
ParserTitle["justify"] = "center"
ParserTitle["text"] = "Parser"
ParserTitle.place(relx=0.05,rely=0.05,width=450,height=100)
PickedFileName=tk.Label(root)
PickedFileName["font"] = 'Bahnschrift', 24
PickedFileName["fg"] = "#333333"
PickedFileName["justify"] = "center"
PickedFileName["text"] = ""
PickedFileName.place(x=150,y=150,width=200,height=60)
PickFileButton=tk.Button(root)
PickFileButton["bg"] = "#efefef"
ft = tkFont.Font(family='Times',size=10)
PickFileButton["font"] = ft
PickFileButton["fg"] = "#000000"
PickFileButton["justify"] = "center"
PickFileButton["text"] = "Select File"
PickFileButton.place(x=150,y=200,width=200,height=46)
PickFileButton["command"] = self.PickFileButton_command
ParseFileButton=tk.Button(root)
ParseFileButton["bg"] = "#efefef"
ft = tkFont.Font(family='Times',size=10)
ParseFileButton["font"] = ft
ParseFileButton["fg"] = "#000000"
ParseFileButton["justify"] = "center"
ParseFileButton["text"] = "Parse"
ParseFileButton.place(x=170,y=330,width=160,height=38)
ParseFileButton["command"] = self.ParseFileButton_command
def PickFileButton_command(self):
global filepath
filepath = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select Log File",
filetypes =(("logs", "*.log"),("All Files", "*.*")))
global fileName
fileName = os.path.basename(filepath)
global fileValidity
print(fileName)
if fileName.endswith('.log') == True:
statLogs = lp.logParse(filepath)
global gameStats
gameStats = statLogs[0]
global playerStats
playerStats = statLogs[1]
fileValidity = True
label.config()
else:
messagebox.showerror("Invalid File", "Invalid file or no file submitted. PLease select a .log file.")
fileValidity = False
def ParseFileButton_command(self):
if fileValidity == True:
global playerStats
playerStats = lc.statCalculations(playerStats, gameStats)
lo.finalOutput(fileName, gameStats, playerStats)
else:
messagebox.showerror("Invalid File", "Invalid file or no file submitted. Please select a .log file before parsing.")
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
我是不是走错路了?谢谢你的阅读
在init函数中,通过将
self.
添加到小部件实例的开头,将所有小部件实例更改为类属性。比如说变成
然后,修改
PickFileButton_command
方法以更改到
这会将标签文本更新为选定文件名的文本
相关问题 更多 >
编程相关推荐