Problems with creating a window using tkinter - python

I am trying to create a program using tkinter and it keeps on giving me this one error:
in __init__ self.master = TK()
NameError: name 'TK' is not defined
I am not sure why it is saying that TK isn't defined when I am importing tkinter, can someone please explain what I am doing wrong.
Here is my code:
from tkinter import *
class App:
def __init__(self):
self.master = TK()
frame = Frame(self.master)
frame.pack()
self.master.minsize(1080,720)
self.master.maxsize(1080,720)
self.master.title("Music Player")
myapp = App()
myapp.mainloop()

It's Tk, not TK. Take a look at this small code given in the documentation of tkinter. The last three lines are here for you.
import tkinter as tk
...
root = tk.Tk()
app = Application(master=root)
app.mainloop()
As a matter of fact, I think you were trying the code from the documentation page, yet you missed it!

It shouldn't be TK; it should be Tk.

Related

Why there is a problem while displaying image from different a GUI in different module by making call to the function from another module?

I tried to make a module in which I made a funtion which just reads and display the image in GUI. Then I made another module which makes call to that function when the button is clicked. Button gives me error.
#module code:
from tkinter import *
class disp:
def __init__(self):
root1.geometry("400x500")
image = PhotoImage(file = 'png2.png')
Label(root1,image=image).pack()
root1.mainloop()
#main code:
from tkinter import *
import testimg as ti
def click():
ti.disp()
root = Tk()
Button(text = 'Click me',command=click).pack()
root.mainloop()
In your class disp, you have put the master as root1 whereas in the main code, you have defined Tk() as root. This means that root1 is no window so the label that has a master of root1 has no where to pack itself.
You also need to remove root1.mainloop() because it’s useless and causing errors due to the fact that root1 doesn’t have Tk(). It’s like trying to loop a while statement without typing in while. This gives an error.
Below modified code is based on yours:
#module code:
from tkinter import *
class disp:
def __init__(self):
root1 = Tk()
root1.geometry("400x500")
image = PhotoImage(master=root1, file='png2.png') # set master to root1
Label(root1, image=image).pack()
root1.mainloop()
But using multiple Tk() instances is not a good design.

Running an external program through a tkinter button in python

Im new to programming, and really only doing this for a school project. Im trying to make a GUI that has a series of buttons that when pressed will run a specific emulator. When I try to run this I get a error saying "z26" is undefined. Im not quite sure on how to actually define it.
from tkinter import *
import os
class Application(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self._button = Button(self, text = "Atari", command = self._openFile)
self._button.grid()
def _openFile(self):
os.startfile(z26.exe)
root = Tk()
root.title("Arcade")
root.geometry("200x85")
app = Application(root)
root.mainloop()
The problem is that you are using x26.exe as a literal, and it is getting evaluated as though it were part of the Python program itself.
Instead, put the path with quotequotations, to make it a string:
os.startfile('path/z26.exe')
See also the Python documentation for os.startfile(path[, operation]).

inheriting ttk instead of Tk

I am building an application where I set up a class 'App' and pass the root when creating an App object.
Class App(root):
.....
.....
def main():
root = Tk()
app = App(root)
Then I seen examples where instead of the above setup, the class was inheriting Tk.
import tkinter as tki
class App(tki.Tk):
"""Project Engineer Release Help Tool"""
def __init__(self):
tki.Tk.__init__(self)
I thought this was much cleaner so I implemented the cleaner version. It all works fine but I have been using ttk.Style() and wanted to inherit ttk. I can't seem to get this to work though. Any help welcome.
Edit for clarity (and typo in the first example):
I was doing the following. Note: using ttk.Entry.
from tkinter import ttk
class App():
def __init__(self, root):
self.root = root
self.entry = ttk.Entry(self.root, width=40)
self.entry.grid(sticky='n', padx=6, pady=12, columnspan=2)
self.entry.bind("<Return>", self.return_click)
self.entry.focus()
root = Tk()
app = App(root)
.......
.......
I changed to inheriting Tk and the style/theme of all my widgets have changed. I would like to stick with inheriting from tkinter but I want to use the ttk themed widgets. How would this be achieved ?

The first argument for python Tkinter

I am using Tkinter with python 2.7 and am curious about why the following code snippet would work:
import Tkinter as tk
import ttk
class Application(ttk.Frame):
def __init__(self, master=None):
ttk.Frame.__init__(self, master) # This is where my question is
self.grid()
return
if __name__ == '__main__':
root = tk.Tk()
app = Application(root)
root.mainloop()
1) The ttk.Frame.__init__ takes one argument, which is the master. But now the first argument is an instance inherited from it, and the second is master. How did this work?
2) I noticed that the ttk.Frame class also have a function called mainloop. How is this different from root.mainloop()?
Thanks!
1) ttk.Frame.__init__() in method Application.__init__() is used to initialize base class ttk.Frame, the explaination doc is here.
2) mainloop() in ttk.Frame and root.mainloop() is equal, please look at this.

Python Tkinter Root Title doesn't work

I can't seem to title my windows. They all have the title "Tk".I believe my code is correct, so correct me if this is wrong...
from Tkinter import *
root = Tk()
root.title="Title"
root.mainloop()
The title is still Tk(). Could I maybe from Tkinter import Tk as MyTitle?
root.title("Title")
Try that, its a method you invoke and pass in the parameter.

Categories

Resources