I am having a struggle with my Python Tkinter GUI. I would like to update the text of PickedFileName to change to the fileName found in PickFileButton(). Results online indicate I can achieve this using StringVar() or .config(), but neither option seems to cooperate. Using StringVar() only renders the text as py_var0, and using .config() does not seem to recognise PickedFileName (i.e. not defined) when I run the code. I would also like to create a similar label of '[filename] generated' once ParseFileButton is pressed where [filename.log] would become [filename.txt], but this also sees similar issues. Everything else seems to function as accordingly. I have attached a snippet of the relevant code below.
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()
Am I approaching this wrong? Thank you for reading.
CodePudding user response:
In your init function change all of your widget instances to be class attributes by adding self.
to the start of them. For example
PickedFileName=tk.Label(root)
becomes
self.PickedFileName=tk.Label(root)
Then, modify your PickFileButton_command
method to change
label.config()
to
self.PickedFileName["text"] = fileName
This will update the label's text to be that of the selected file name.