i am trying to make a program using the tkinter library in python but it gives error showing that ---
NameError: name 'menubar' is not defined
import tkinter
import sys
def hey():
print("hello")
def myNew():
mlabel = Label(root,text="yo").pack()
root = tkinter.Tk()
root.title("Wizelane")
root.geometry('400x80+350+340')
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="New",command=myNew)
label = tkinter.Label(root,text="say hello")
label.pack()
hello = tkinter.Button(root,text="hello",command=hey)
hello.pack()
root.mainloop()
You are missing some important parts here.
You need to configure the menu first and you also need to add the cascade label.
Take a look at this code.
import tkinter
def hey():
print("hello")
def myNew():
# you forgot to use tkinter.Label here.
mlabel = tkinter.Label(root, text="yo").pack()
root = tkinter.Tk()
root.title("Wizelane")
root.geometry('400x80+350+340')
my_menu = tkinter.Menu(root)
# configure root to use my_menu widget.
root.config(menu = my_menu)
# create a menu widget to place on the menubar
file_menu = tkinter.Menu(my_menu, tearoff=0)
# add the File cascade option for drop down use
my_menu.add_cascade(label = "File", menu = file_menu)
# then add the command you want to add as a File option.
file_menu.add_command(label="New", command = myNew)
label = tkinter.Label(root, text="say hello")
label.pack()
hello = tkinter.Button(root, text="hello", command = hey)
hello.pack()
root.mainloop()
Related
When program starts with the default size, for example 10x10, in the size submenu the checkmark should already be in front of the 10x10 line. Need to initially specify one of the options, and then to be able to choose any option.
from tkinter import Tk, Menu
root = Tk()
menubar = Menu(root)
size = Menu(menubar, tearoff=0)
size.add_radiobutton(label='5x5')
size.add_radiobutton(label='10x10') # <- Checkmark must be here when program starts.
# When choosing another option, it must be unmarked,
# like in this example
size.add_radiobutton(label='15x15')
menubar.add_cascade(label='Size', menu=size)
root.config(menu=menubar)
root.mainloop()
The radiobuttons need a Tk variable to group the buttons. The code below uses an IntVar. The result is reported in a Label.
import tkinter as tk
root = tk.Tk()
root.geometry( '100x100')
radio_var = tk.IntVar( value = 10 ) # Option 10 is the default.
menubar = tk.Menu(root)
size = tk.Menu(menubar, tearoff=0)
size.add_radiobutton(label='5x5', variable = radio_var, value = 5 )
size.add_radiobutton(label='10x10', variable = radio_var, value = 10 )
size.add_radiobutton(label='15x15', variable = radio_var, value = 15 )
menubar.add_cascade(label='Size', menu=size)
root.config(menu=menubar)
lab = tk.Label( root, textvariable = radio_var )
lab.grid()
root.mainloop()
Do not add_radiobutton at the start. Only you can add add_radiobutton to submenu. And create another subemenu1, submenu2, etc.
Code:
from tkinter import Tk, Frame, Menu
root = Tk()
root.title("Submenu")
menubar = Menu(root)
root.config(menu=menubar)
_menu = Menu(menubar, tearoff=0)
submenu1 = Menu(_menu, tearoff=0)
submenu1.add_radiobutton(label="5x5")
submenu1.add_radiobutton(label="OPtion 3")
submenu1.add_radiobutton(label="Option 4")
submenu = Menu(_menu, tearoff=0 )
submenu.add_radiobutton(label="10x10")
submenu.add_radiobutton(label="Option 1")
submenu.add_radiobutton(label="Option 2")
_menu .add_cascade(label="5x5", menu=submenu1)
_menu .add_cascade(label='10x10', menu=submenu)
_menu .add_cascade(label="15x15")
menubar.add_cascade(label="Size", menu=_menu )
root.mainloop()
I'm using tkinter.
I have a button to create a label with text in it. I want to clear that label with another button.
How can i do that?
I'm pretty new on tkinter. ıs there somebody to lead me
Here are a few options
Option 1
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text='Clear Me!')
label.pack()
# define a function to set the label text to an empty string
def clear_label_text():
label.config(text='')
# set up a button to call the above function
btn_clear = tk.Button(
root,
text='Click to Clear Label',
command=clear_label_text # note the lack of '()' here!
)
btn_clear.pack()
if __name__ == '__main__':
root.mainloop() # start the app
Option 2
This is a similar approach, but instead of defining a function to clear the label, we'll use a lambda (an anonymous function)
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text='Clear Me!')
label.pack()
# set up a button to clear the label
btn_clear = tk.Button(
root,
text='Click to Clear Label',
# lambdas make for clean code, but don't go crazy!
command=lambda: label.config(text='')
)
btn_clear.pack()
if __name__ == '__main__':
root.mainloop() # start the app
Neither of these methods will outright destroy the label, so you can set it's text again at any time a la label.config(text='New and improved text!')
from tkinter import *
root = Tk()
lab1 = Label(root, text = "Hello World")
lab1.pack()
but1 = Button(root, text = "clear", command=lab1.destroy)
but1.pack()
root.mainloop()
Hi I am pretty new to tkinter and have being trying to create a button that opens a window then a have a button in the new window the gives a message when pressed. I ran into the problem that the only whay I could get it to recognise the function I wrote was to write it inside the function that opens the second window. I don't know if I have being searching for the wrong things but I can't find how to do this properly. Can someone help me out Here is my code
from tkinter import *
master = Tk()
master.title("frame control")
def win():
window2 = Toplevel()
def open():
stamp = Label(window2, text="Staped").pack()
lab2 = Button(window2,text = "yo ",command = open).pack()
lab1 = Button(master,text = " open a new window" , command = win).pack()
mainloop()
This is your code but with best practises:
import tkinter as tk
def create_stamp():
stamp = tk.Label(window2, text="Stamp")
stamp.pack()
def create_second_win():
global window2
window2 = tk.Toplevel(root)
lab2 = tk.Button(window2, text="Click me", command=create_stamp)
lab2.pack()
root = tk.Tk()
root.title("Frame control")
button = tk.Button(root, text="Open a new window", command=create_second_win)
button.pack()
root.mainloop()
I made window2 a global variable so that I can access it from create_stamp. Generally it is discouraged to use from ... import *. As #Matiiss said, sometimes you can have problems with global variables if you don't keep track of the variable names that you used.
If you want to avoid using global variables and want to use classes, look at this:
import tkinter as tk
class App:
def __init__(self):
self.stamps = []
self.root = tk.Tk()
self.root.title("Frame control")
self.button = tk.Button(self.root, text="Open a new window", command=self.create_second_win)
self.button.pack()
def create_stamp(self):
stamp = tk.Label(self.window2, text="Stamp")
stamp.pack()
self.stamps.append(stamp)
def create_second_win(self):
self.window2 = tk.Toplevel(self.root)
self.lab2 = tk.Button(self.window2, text="Click me", command=self.create_stamp)
self.lab2.pack()
def mainloop(self):
self.root.mainloop()
if __name__ == "__main__":
app = App()
app.mainloop()
As #Matiiss mentioned it would be more organised if you move the second window to its own class. For bigger projects it is a must but in this case you don't have to.
When the user didn't chose any option, the code going into else, in if_button_ispressed function, and then I want to print an error message on the screen. I think I did all correct but the message is not shown on the screen.
import sys
from Tkinter import *
import Image, ImageTk
import ShareScreen
import Menu_WatchAnothersScreen
def if_button_is_pressed():
global user_selection, mGui
if(user_selection.get() == 1): # if the user want to share his screen
mGui.destroy()
ShareScreen.run()
#menu()
if(user_selection.get() == 2): # if the user want to watch another`s screen
mGui.destroy()
Menu_WatchAnothersScreen.run()
#menu()
else: # if the user didn`t chose any option <--------------- HERE IS MY PROBLEM
error_message = "Please select one of the options" #<--------------- HERE IS MY PROBLEM
error_label = Label(mGui, textvariable=error_message).pack(anchor=CENTER) # prints error message <--------------- HERE IS MY PROBLEM
def close(): # close the window
exit()
def menu():
global user_selection,mGui
mGui = Tk()
user_selection = IntVar()
menubar = Menu(mGui) # menu
filemenu = Menu(menubar, tearoff=0) # menu works
filemenu.add_command(label="Close", command=close)
menubar.add_cascade(label="File", menu=filemenu)
mGui.geometry('450x300+500+300')
mGui.title('Nir`s ScreenShare') # top of window
canvas = Canvas(mGui, width=500, height=150)
canvas.pack(pady = 10)
pilImage = Image.open("logo5.png")
image = ImageTk.PhotoImage(pilImage)# puts ScreenirShare`s logo on the menu
imagesprite = canvas.create_image(0, 0, image=image, anchor="nw")
Radiobutton(mGui, text="Share My Screen ", variable=user_selection, value=1).pack(anchor=CENTER) # 1 - FIRST OPTION - Share My Screen
Radiobutton(mGui, text="Watch Another`s Screen", variable=user_selection, value=2).pack(anchor=CENTER, pady = 7.5)# 2- SECOND OPTION - Watch Another`s Screen
start_button = Button(mGui, text='Start', command=if_button_is_pressed).pack() # Start Button
mGui.config(menu=menubar) # menu helper
mGui.mainloop()
menu()
If you are going to use plain string, you should use text option of label not textvariable.
Label(mGui, text=error_message)
If you want to use textvariable, you need StringVar, IntVar etc.
Also, packing on same line returns None, so you should pack after you create it if you want to use it again later.
error_label = Label(mGui, textvariable=error_message)
error_label.pack(anchor=CENTER)
I have a bit of difficulty with the code below. Basically, I want the code to, when I press the Enter button, to open the window2 but also close window1 simultaneously so that there is only one window and not two of them.
The code is...
from tkinter import *
def window1():
window = Tk()
window.title("Welcome")
f = Frame()
f.pack()
label1 = Label(window, text = "Welcome to the random window")
label1.pack()
button1 = Button(window, text = "Enter...", command = window2)
button1.pack()
def window2():
screen = Tk()
screen.title("Pop-Up!")
fr = Frame()
fr.pack()
label2 = Label(screen, text = "This is a pop-up screen!")
label2.pack()
button2 = Button(screen, text = "Return", command = window1)
button2.pack()
window1()
This is "Bad" because you're using two instances of Tk. Try instead using TopLevels.
import tkinter as tk
def window1():
window = tk.Toplevel(root)
window.title("Welcome")
# etc etc ...
tk.Button(window,text="Enter...",command=lambda: window2(window)).pack()
def window2(old_window):
old_window.destroy()
# window2 stuff
root = tk.Tk()
root.iconify() # to minimize it, since we're just using Toplevels on top of it
window1()
root.mainloop()
When you are using the Tk() function, you are creating a new instance of the Tcl/tkinter interpreter. Instead use Toplevel() which will make a new window in the current interpreter.