problem openning a window in python using tkinter with button [duplicate] - python

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
everyone, I am using Tkinter and I want to open a window after clicking on the button but I have this problem that when I run the program it will open the window not the button and the button is completely useless
this is my code
import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
def addApp():
filename = filedialog.askopenfilename(initialdir="/", title="Select File",
filetypes=(("executable", "*.exe"), ("allfiles", "*.*")))
canvas = tk.Canvas(root, height=700, width=700, bg="#263D42")
canvas.pack()
frame = tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
openFile = tk.Button(root, text="OpenFile", padx=10,
pady=5, fg="white", bg="#263D42", command=addApp())
openFile.pack()
runApps = tk.Button(root, text="RunApps", padx=10, pady=5, fg="white", bg="#263D42")
runApps.pack()
root.mainloop()

Rename command=addApp() to command=addApp

Related

Why does my button not display in GUI (Python,TK)

I am trying to create a simple GUI in Python to create buttons to access apps within my computer.
I am very new to Python & coding in general so bear with me..
I am getting 0 error messages when running the code but it just isn't displaying the button at the bottom. Here's what I have written out.
Here's a screenshot of my result
import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
canvas = tk.Canvas(root, height=700, width=700, bg="#263D42")
canvas.pack()
frame = tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
openFile = tk.Button(root, text="Open File", padx=10,
pady=5, fg="white", bg="#263D42")
openFile.pack()
root.mainloop()

Thread in Tkinter starts automatically [duplicate]

This question already has an answer here:
Tkinter Button command getting executed before clicking the button [duplicate]
(1 answer)
Closed 2 years ago.
I am trying to add a thread to tkinter button to trigger when its presses. but as soon as I run the program, the function inside the button starts automatically before I click anything.
Here is my code:
m = Main()
root = Tk()
root.geometry("500x500")
frame = Frame(root)
frame.pack()
start = Button(frame, text="Start",
command=threading.Thread(target=m.main).start())
stop = Button(frame, text="stop", command=quit)
start.pack(pady=20)
stop.pack()
root.mainloop()
The reason why the function runs without you clicking the button is because you are adding parentheses after the function.
So you would need to change this line:
start = Button(frame, text="Start",
command=threading.Thread(target=m.main).start())
to this:
start = Button(frame, text="Start",
command=threading.Thread(target=m.main).start)
All you have to do is take out the parentheses, which tells python that you only run the function if the button is clicked. If you include the parentheses, python takes it as a normal function call and ignores the button completely.
Full code: as per what you gave in your question
m = Main()
root = Tk()
root.geometry("500x500")
frame = Frame(root)
frame.pack()
start = Button(frame, text="Start",
command=threading.Thread(target=m.main).start)
stop = Button(frame, text="stop", command=quit)
start.pack(pady=20)
stop.pack()
root.mainloop()
or you could use lambda.

Destroy window not closing all windows properly

I have two queries in this section.
In my code i have created two frames under root, the first frame have "NEXT" button to go on second frame. In second frame there is Run button, which has mapped with close_window function. It should close all windows properly. But in my case not closing it.
When i click "Run" i need to close all windows and need to execute another script on the same directory. Is that possible to do it ?
from Tkinter import *
def close_window():
frame2.destroy()
frame1.destroy()
def swap_frame(frame):
frame.tkraise()
root = Tk()
root.geometry("900x650+220+20")
root.title("Testing")
root.configure(borderwidth="1", relief="sunken", cursor="arrow", background="#dbd8d7", highlightcolor="black")
root.resizable(width=False, height=False)
frame2 = Frame(root, width=900, height=650)
frame1 = Frame(root, width=900, height=650)
Button1 = Button(frame1, text="Next", width=10, height=2, bg="#dbd8d7", command=lambda: swap_frame(frame2))
Button1.place(x=580, y=580)
Button2 = Button(frame2, text="Run", width=10, height=2, bg="#dbd8d7", command=close_window,)
Button2.place(x=580, y=580)
frame2.grid(row=0, column=0)
frame1.grid(row=0, column=0)
root.mainloop()
what is wrong in the code?
"Destroy window not closing all windows properly"
Nor should it. destroy method destroys a widget, and when a widget is destroyed so are its children.
Since neither frame1 nor frame2 are 'windows' or a window's parents, there's no window destruction taking place.
"When I click "Run" I need to close all windows and need to execute another script in the same directory. Is that possible to do it?"
It is possible. Use quit on any GUI object, instead of destroy. It stops the mainloop, hence destroying the entire GUI. In that it solves the first problem as well. Then import another_script:
...
def close_window():
frame1.quit()
...
root.mainloop()
import another_script

python tkinter clicking on button to open new window

I've made 3 buttons on my window. I choosed that the main window should have a specific background image and a full screen.
Now there is a problem. I would like to move to a new window (page) (with an other background and other things) by clicking on button 3.
Things i tryd:
from Main.Info.travelhistry import *
I've added this to the main window to open a new python file with the code of the second screen that has to open when clicking on button 3. But I found out that if I do this both windows will open when running main window.
I added root1 = Tk() at the beginning, root1.mainloop() at the end and between them the code for the other window. But this won't work also, its opening 2 windows like above.
Those were all my attempts and i cant figure out a better way. I can but the background would stay the same. But I have to change the background for the new window to a background image i made...
Any idea what im doing wrong?
from tkinter import *
from tkinter.messagebox import showinfo
from Main.Info.travelhistry import *
def clicked1():
bericht = 'Deze functie is uitgeschakeld.'
showinfo(title='popup', message=bericht)
root = Tk()
a = root.wm_attributes('-fullscreen', 1)
#Hoofdmenu achtergrond
C = Canvas(root, bg="blue", height=250, width=300)
filename = PhotoImage(file = "test1.png")
background_label = Label(root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
# Geen OV-chipkaart button
b=Button(master=root, command=clicked1)
photo=PhotoImage(file="button1.png")
b.config(image=photo,width="136",height="53", background='black')
b.place(x=310, y=340)
#Buitenland button
b2=Button(master=root, command=clicked1)
photo1=PhotoImage(file="button2.png")
b2.config(image=photo1,width="136",height="53", background='black')
b2.place(x=490, y=340)
#Reis informatie
b3=Button(master=root)
photo2=PhotoImage(file="button3.png")
b3.config(image=photo2,width="136",height="53", background='black')
b3.place(x=680, y=340)
root.mainloop()
root2.mainloop()
You shouldn't call more than one Tk() window.
Instead, tkinter has another widget called Toplevel which can be used to generate a new window.
See below for an example:
from tkinter import *
root = Tk()
def command():
Toplevel(root)
button = Button(root, text="New Window", command=command)
button.pack()
root.mainloop()
This one opens new window that you can edit.
from tkinter import *
Window = Tk()
def Open():
New_Window = Tk()
#You can edit here.
New_Window.mainloop()
Btn1 = Button(text="Open", command=Open)
Bt1n.pack()
Window.mainloop()

Choosing file by clicking on a button [Python 2.7.6]

I'm trying to create a simple button that allows me to choose files such as text doc/pictures(jpg/png). I tried searching for answers here but didn't had any luck. I'm using Tkinter for my GUI interface.
This are my codes so far.
from Tkinter import *
root = Tk()
root.title("Hashing Tool")
root.geometry("600x300")
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
button = Button(frame, text="Choose File", fg="black")
button.pack( side = BOTTOM)
from tkFileDialog import askopenfilename
filename = askopenfilename()
print(filename)
root.mainloop()
Currently, you ask for a file as soon as the program starts. You have to put that part of the code into a callback function and pass that to the button's command parameter.
def getfile():
filename = askopenfilename()
print(filename)
button = Button(frame, text="Choose File", fg="black", command=getfile)

Categories