I am programming a menu into my first python tkinter menu window. I have done it all right (I think) but the menu doesn't appear on my tkinter window.
My code is:
from tkinter import *
def f1():
label = Label(window, text="Wassup CHUNGUS!!!")
label.grid(row=0, column=0, sticky=W)
global window
window = Tk()
window.title("CHUNGUS")
label2 = Label(window, text="!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
label2.grid(row=2, column=0, sticky=W)
menubar = Menu(window)
firstmenu = Menu(menubar, tearoff=0)
firstmenu.add_command(label="EXIT", command=window.destroy)
firstmenu.add_command(label="CHUNGUS", command =f1)
menubar.add_cascade(label="Menu", menu=firstmenu)
window.mainloop()
Can I have some help?
This is quite easy. You have not included window.config(menu=menubar). You should put it before the window.mainloop()
so:
window.config(menu=menubar)
window.mainloop()
Just put the code window.config(menu=menubar) before window.mainloop()
you forgot to config the code to menu if you understand window.config(menu=menubar)
and also a tip ig in this code you don't need global window if for this code im sayin
The simplest way to achieve a drop-down list within Tkinter is to use the OptionMenu widget built into tkinter.
from Tkinter import *
master = Tk()
variable = StringVar(master)
variable.set("one") # default value
w = OptionMenu(master, variable, "one", "two", "three")
w.pack()
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 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().
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.
I have this snippet of code and I expect it to create a gui with a bunch of buttons and a text box. But I only see the empty box with the title:
from tkinter import *
root = Tk()
root.title("title")
root.mainloop()
button1 = Button(root, text="button1")
button2 = Button(root, text="button2")
button3 = Button(root, text="button3")
text = Entry(root)
listbox = Listbox(root)
text.pack()
button1.pack()
button2.pack()
button3.pack()
listbox.pack()
Is it because of inconsistencies between different versions of Python? I am trying to learn about Tkinter using this quick guide
You have to move the call to root.mainloop() to the end of the file.
You have to do root.mainloop() at the end to get the code.
I'd like to set the focus of my program to a specific entry widget so that I can start entering data straight-away - how can I do this?
My current code
from Tkinter import *
root = Tk()
frame=Frame(root,width=100,heigh=100,bd=11)
frame.pack()
label = Label(frame,text="Enter a digit that you guessed:").pack()
entry= Entry(frame,bd=4)
entry.pack()
button1=Button(root,width=4,height=1,text='ok')
button1.pack()
root.mainloop()
Use entry.focus():
from Tkinter import *
root = Tk()
frame=Frame(root,width=100,heigh=100,bd=11)
frame.pack()
label = Label(frame,text="Enter a digit that you guessed:").pack()
entry= Entry(frame,bd=4)
entry.pack()
entry.focus()
button1=Button(root,width=4,height=1,text='ok')
button1.pack()
root.mainloop()
I tried this way and it is ok
entry= Entry(root)
entry.focus()
entry.pack