Running an external program through a tkinter button in python - 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]).

Related

Is there a way to make a label show in every window for tkinter without having to write out every single line of code over and over

I am wondering if there is a way, in tkinter or using any other python module, to make it so you keep a label or any other element in every window made by just using something like a function that makes the label within the window? I've tried this:
#Modules
import tkinter as tkin
#The initializer window
class Test:
def __init__(self):
#Initializes the main window
self.start = tkin.Tk()
self.start.title('Test')
self.label_thing()
#Makes a label
def label_thing(self):
self.label1 = tkin.Label(text='Not Button')
self.label1.pack()
I don't know if I made any errors or if this isn't a thing you can do but I'd also like to refrain from having the label localized to this window and having to remake the code for every window.
Let us assume you have a button that creates windows, you would pass this window as an argument to the function that creates the label, so like:
import tkinter as tk # Fixed weird import naming
class Test:
def __init__(self):
self.start = tk.Tk()
self.start.title('Test')
self.label_thing(self.start) # Label on the main window
tk.Button(self.start,text='Click me',command=self.create_win).pack()
self.start.mainloop()
def label_thing(self, master):
self.label1 = tk.Label(master, text='Not Button')
self.label1.pack()
def create_win(self):
new = tk.Toplevel()
self.label_thing(new) # Label on the new window
if __name__ == '__main__':
Test()
As you can see, as long as you press the button, new windows are created, and all those windows have the label on them, dynamically.

Function not working properly when called by a method : Python

So when I am calling the list directory code below directly then it's working fine. But when I am calling main.py which uses app class from gui which calls the list directory function, It prints "yoo" but prints an empty list instead of list of directories. I am stuck and can't figure out why that's happening. Any ideas?
Outputs:
When list directory called directly :
["/home/shubham/Desktop/movies/djangounchained.mkv"]
"yoo"
When called by main.py with same argument:
[]
"yoo"
Here is my main script
from gui import app
from list_directory import display_files
import tkinter as tk
root = tk.Tk()
directory = input("Enter directory name:")
root.geometry("400x300")
widgets_creator = app(root)
name = "get list"
directory_button = widgets_creator.create_button(name,function=display_files,path=directory)
root.mainloop()
Here is my gui script
import tkinter as tk
class app(tk.Frame):
def __init__(self,master):
super(app,self).__init__(master=master)
self.master = master
self.init_window()
def init_window(self):
# changing the title of our master widget
self.master.title("GUI")
# allowing the widget to take the full space of the root window
self.pack(fill=tk.BOTH, expand=1)
# creating a button instance
quitButton = tk.Button(self, text="Quit")
# placing the button on my window
quitButton.place(x=0, y=0)
def create_button(self,button_name,function,path):
button = tk.Button(self.master,text=button_name,command=lambda: function(path))
button.place(x=200,y=5)
return button
Here is my list_directory code:
import glob
def display_files(path):
x = glob.glob(path)
print(x)
print("yoo")
if __name__ == '__main__':
display_files("/home/shubham/Desktop/movies/*")
I might have found your problem. The code works fine, the problem is your argument. For example, if I enter '/Users/rudy/Desktop/*' when the input prompt comes up, I have the same result as you.
However, when I enter /Users/rudy/Desktop/* (without quotes), everything works fine. input() already saves the input as a string, so you don't need to add additional quotes.

Inconsistent Tkinter behavior at runtime after code changes

Note: I am not a programmer by trade or education, so bear with me. Take the following simple application:
import tkinter as tk
root = tk.Tk()
class app(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.button1= tk.Button(self, text='1')
self.button1.pack(side='top')
self.quit_button= tk.Button(self, text='quit', command=self.quit_button.destroy())
self.quit_button.pack(side='bottom')
application = app(root)
application.mainloop()
When I run this code, I am told destroy() isn't an method of quit_button. If I change it to:
self.quit_button= tk.Button(self, text='quit', command=self.dest)
and I add the method:
def dest(self):
self.quit_button.destroy()
then it works - I assume this has something to do with the button being unable to reference itself while it's being created (please correct/enlighten me on this if you can since I don't fully understand this behavior).
However, what I'm really asking about is that the first time the program is run after I get this error, I get one window with both buttons, and X extra windows with only button1 (packed appropriately) where X is the number of times I incurred the error. Obviously I can just "not do that" but it would be extremely educative for me to understand how tkinter could be behaving this way. Is anyone here familiar enough with OOP to help? I create the root window before anything else, so there should be only one in any case.
You've got a couple of problems in your code. All in the line:
self.quit_button= tk.Button(self, text='quit', command=self.quit_button.destroy())
The first is you're trying to reference self.quit_button at the same time you're creating it. The second, somewhat related issue, is the command=self.quit_button.destroy() part actually tries to call one of this non-existent Button's methods rather than just supplying a reference to it (because of the ()s following its name).
Here's a version with those problems fixed:
import tkinter as tk
root = tk.Tk()
class app(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.button1= tk.Button(self, text='1')
self.button1.pack(side='top')
self.quit_button = tk.Button(self, text='quit')
self.quit_button.config(command=self.quit_button.destroy) # set function to execute
self.quit_button.pack(side='bottom')
application = app(root)
application.mainloop()

Problems with creating a window using tkinter

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.

adding image to tkinter gui

I am new to Tkinter. I want to create a GUI that can support embedding audio files and that also has a background image. I have tried endlessly to install pygame to no avail. I cannot seem to figure out why it is not installing correctly so at this point I am just trying to find the easiest way possible to have these two options. Below is my attempt at displaying a background image using a canvas widget. However, I always get an error that my variables are not defined. I would really appreciate some feedback on what I am doing wrong, as well as any helpful tkinter tutorials that involve more than just the basics. Thanks in advance
from Tkinter import *
root = Tk()
root.geometry("500x500")
class Application(Frame):
def __init__(self, master):
#initialize the frame
Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.can = Canvas(root, width=160, height=160, bg='white')
self.pic = PhotoImage(file='speaker.gif')
self.item = can.create_image(80, 80, image=pic)
app = Application(root)
#kick off event loop
root.mainloop()
Every time you want to use an attribute of a class inside one of its methods, you need to prefix it with self.:
self.item = self.can.create_image(80, 80, image=self.pic)
# ^^^^^ ^^^^^
Otherwise, Python will treat the names as being local to the function and will raise an exception when it fails to find them.
Also, you forgot to call grid on your canvas widget:
self.can = Canvas(root, width=160, height=160, bg='white')
self.can.grid(...)
As for resources on Tkinter, you can check out these:
http://www.tkdocs.com/tutorial/onepage.html
http://effbot.org/tkinterbook/

Categories

Resources