I want to switch the screen using the tkinter module.
Is it possible to switch screens without frame?
This is the code I did.
#frame1.py
from tkinter import *
root=Tk()
root.title('page1')
def goto_p2():
root.destroy()
import frame2
Button(root, text="goto page1", command=goto_p2).pack()
mainloop()
.
#frame2.py
from tkinter import *
root=Tk()
root.title('page1')
def goto_p1():
root.destroy()
import frame1
Button(root, text="goto page2", command=goto_p1).pack()
mainloop()
Here you go, example code how it can be done.
import tkinter as tk
from tkinter import Button,Label
def clear_frame():
for widget in window.winfo_children():
widget.destroy()
def screen_two():
clear_frame()
button_2 = Button(window, text="Go to screen one", command=lambda: screen_one())
button_2.pack(pady=10)
label_2 = Label(window, text="Label on window two")
label_2.pack(pady=10)
def screen_one():
clear_frame()
button_1 = Button(window, text="Go to screen two", command=lambda: screen_two())
button_1.pack(pady=10)
label_1 = Label(window, text="Label on window one")
label_1.pack(pady=10)
window = tk.Tk()
window.title("Test")
window.geometry('250x100')
screen_one()
window.mainloop()
[EDITED]
Function clear_frame() will destroy every widget in frame ( basicly it will clear screen). Besides clear_frame(), there are two more functions, screen_two() and screen_one(). Both of those functions are doing the same thing, calling clear_frame() function, and displaying one button and one label. (Only difference is text inside label and text inside button).
When code runs, it will create window with geometry of 250x100. It will call screen_one() function and run mainloop().
Related
i have 2 python file TestBtnImage1.py and TestBtnImage2.py
I am trying to call display() fucntion of TestBtnImage2 on button click of TestBtnImage1.py,
button without image displying in frame, however button with image is not displying.
thanks in advance
TestBtnImage1.py
from tkinter import *
from TestBtnImage1 import *
frame = Tk()
frame.title("TextBox Input")
frame.geometry('200*200')
frame.config(background="blue")
printButton = Button(frame, text="click me", command=display()).place(x=50, y=50)
frame.mainloop()
TestBtnImage2.py
from tkinter import *
def display():
frame = Tk()
frame.title("TextBox Input")
frame.geometry('200*200')
frame.config(background="blue")
logo1 = PhotoImage(file="cheta.png")
Button(gui, text="click me", image=logo1, bg="blue", relief=FLAT).place(x=100, y=200)
frame.mainloop()
tried googling for the same issue also stackoverflow but no help.
There are a few problems in your code.
In TestBtnImage1.py
You are importing itself, which will cause a NameError.
from TestBtnImage1 import *
and you just need to change it to TestBtnImage2
The geometry specifier should be 'x'
If you use frame.geometry('200*200'), it will cause tkinter bad geometry specifier error.
So use 200x200 instead of 200*200.
The button command
printButton = Button(frame, text="click me", command=display()).place(x=50, y=50)
The display() function will automatically run when executed. You can remove the brackets ()
-> printButton = Button(frame, text="click me", command=display).place(x=50, y=50)
(*) Also, the printButton (None) does nothing. So you can remove it.
In TestBtnImage2.py
Use Toplevel() instead of a new Tk()
-> frame = Toplevel()
Again, bad geometry specifier.
-> frame.geometry('200x200')
Use mainloop() only once.
You can remove frame.mainloop() at the end of the function
(*) Also,
Button(gui, text="click me", image=logo1, bg="blue", relief=FLAT).place(x=100, y=200)
The master gui is not defined.
So, the full code should be:
TestBtnImage1.py:
from tkinter import *
from TestBtnImage2 import *
frame = Tk()
frame.title("TextBox Input")
frame.geometry('200x200')
frame.config(background="blue")
Button(frame, text="click me", command=display).place(x=50, y=50)
frame.mainloop()
TestBtnImage2.py:
from tkinter import *
def display():
frame = Toplevel()
frame.title("TextBox Input")
frame.geometry('200x200')
frame.config(background="blue")
logo1 = PhotoImage(file="cheta.png")
Button(frame, text="click me", image=logo1, bg="blue", relief=FLAT).place(x=100, y=200)
I am making a program that can launch files and programs like a Stream Deck. After selecting the file I want to assign to a button, the button resizes due to the filename being wider than the placeholder text "Add".
I couldn't find any solutions to this problem anywhere.
I am desperate on finding the solution as this is pretty much the final thing i need to fix to make the program pre-Alpha.
Thank you in advace.
How to reproduce this issue:
import tkinter
from tkinter import *
root = Tk()
button1 = Button(root, text="Add", padx=10, pady=10)
button2 = Button(root, text="More Text", padx=10, pady=10)
button1.grid(row=0, column=0)
button2.grid(row=1, column=0)
root.mainloop()
A widget appears in its natural size, if not defined otherwise. The natural size is usally the smallest amount of pixels needed to display the widget in width and height. You can change that appearance by defining a width for your Button, even better have a constant if you use the same value for a couple of Buttons. Example:
import tkinter as tk
#from tkinter import *
#dont't use wildcard imports
#only import your modul once
BUTTONWIDTH = 10 #Constant
root = tk.Tk()
button1 = tk.Button(root, text="Add", width=BUTTONWIDTH) #use width
button2 = tk.Button(root, text="More Text", width=BUTTONWIDTH)
button1.grid(row=0, column=0)
button2.grid(row=1, column=0)
root.mainloop()
I have 2 frames, one of them has a button "click". On clicking that button, the same button should be destroyed from the original frame and move to frame2. How to achieve it using tkinter.
there is tow frames frame1 and frame2 first we want to show this button using pack(). now we want to add function on command of my_btn then we want to destroy that button and re define it in the second frame.
final code:
from tkinter import *
window = Tk()
frame1 = Frame(window, width=50, height=50, bg='red')
frame2 = Frame(window, width=50, height=50, bg='blue')
frame1.propagate(0); frame2.propagate(0)
frame1.pack(); frame2.pack()
def click():
global my_btn
my_btn.destroy()
my_btn = Button(master=frame2)
my_btn.pack()
my_btn = Button(master=frame1, command=click)
my_btn.pack()
window.mainloop()
Hope this works.
So I can do a button in a simple window but not on a Canvas.
from tkinter import *
window = Tk()
def function():
print('Hello World')
tk_button = Button(window, text = 'Click me!', command = function)
I want to position the button on a tkinter Canvas and have graphics around it, not just the bare window with the button.
You can use the create_window function on a canvas to put a frame on it, then use that frame to pack other widgets normally.
import tkinter as tk
mw = tk.Tk()
canvas = tk.Canvas(mw, bg='grey75')
canvas.pack()
frame = tk.Frame(canvas, width=50, height=5)
canvas.create_window((1,1), window=frame, anchor='nw')
button = tk.Button(frame, text='Hello World')
button.pack()
mw.mainloop()
I am using python 3 I want to close an tkinter window to continue in code, but it doesn't work.
Here is my code:
import tkinter as tk
from tkinter import *
from tkinter import messagebox
def window():
global frame
frame = Tk()
Button1 = tk.Button(frame, text="No.", command=frame.quit)
Button1.pack(anchor=S, fill=X, side=RIGHT)
Button2 = tk.Button(frame, text="Yes!", fg="dark green", command=func)
Button2.pack(anchor=S, fill=X, side=LEFT)
frame.mainloop()
def func():
frame.destroy()
frame.quit()
messagebox.showinfo("Help", "Please help me.")
#Next step ....
window()
I want to exit the script with Button1 and continue and close the window with Button2, but I can't close the window in use of an other function.
import tkinter as tk
from tkinter import messagebox
def window():
global frame
frame = tk.Tk()
Button1 = tk.Button(frame, text="No.", command=func)
Button1.pack(anchor=tk.S, fill=tk.X, side=tk.RIGHT)
Button2 = tk.Button(frame, text="Yes!", fg="dark green", command=func)
Button2.pack(anchor=tk.S, fill=tk.X, side=tk.LEFT)
frame.mainloop()
def func():
global frame
frame.destroy()
messagebox.showinfo("Help", "Please help me.")
#Next step ....
window()
I have solved it,
the problem was: Evry messagebox need an main window, I have destroyed this main window before starting the massagebox and the messagebox started an new emty main window. My resulution is to minimize the main window, start the messagebox and after that to close the massagebox for cotinueing in code.
from tkinter import *
from tkinter import messagebox
main = Tk()
main.geometry("500x400+300+300")
def message():
main.geometry("0x0")
messagebox.showwarning("Say Hello", "Hello World")
main.destroy()
B1 = Button(main, text = "Start Dialog",fg="dark green", command = message)
B1.pack()
main.mainloop()
print("finish dialog")