I am trying to create a GUI application (just starting), but I have run into a problem.
When i use .pack() (which i need to use) the button is not getting displayed, but if i use .grid(), it does display.
This is the code:
class TemperaturePlotApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent)
self.master.title("Max Temperature")
self.button = Button(self, text="Browse", command=self.load_file,
width=10, *args, **kwargs)
self.button.pack(side="left", fill="both", expand=True)
def load_file(self):
fname = askopenfilename(filetypes=(("Text File", "*.txt")))
if fname:
try:
print("""here it comes: self.settings["template"].set(fname)""")
except: # <- naked except is a bad idea
showerror("Open Source File", "Failed to read file\n'%s'" % fname)
return
def main():
root = tk.Tk()
app = TemperaturePlotApp(root)
root.geometry("800x400")
root.mainloop()
if __name__ == '__main__':
main()
If i use .grid(), it works:
self.master.rowconfigure(5, weight=1)
self.master.columnconfigure(5, weight=1)
self.grid(sticky=W+E+N+S)
self.button = Button(self, text="Browse", command=self.load_file, width=10)
self.button.grid(row=1, column=0, sticky=W)
I have to use .pack(), not grid, so if someone could please explain what i am doing wrong, i would greatly appreciate it.
Thanks Corey
Your main problem is that you are not packing your TemperaturePlotApp
widget (which derives from a Frame
). Try the following code:
...
app = TemperaturePlotApp(root)
app.pack()
...
Notice also that you are probably importing tkinter in the following way:
import tkinter as tk
Because you are inheriting from a Frame
widget using:
class TemperaturePlotApp(tk.Frame):
On the other hand, you are trying to create a button using:
self.button = Button(...)
Which means that either you are using another button from another library (for example ttk
) or you are having conflicts or something similar. You might want it like:
self.button = tk.Button(...)