tkinter: LabelFrame in a separate TopLevel window - python

My "simple" intention is to create a Dialog - launched by a button - where I have several LabelFrames with their radio button.
I tried with 1 LabelFrame with this simple code the radios appear in the main window, not in the dialogue one !!!! I cannot understand why. Please help.. ty for your attention
Here's the code:
import tkinter as tk
from tkinter import ttk
class PersonEmptyDocsDialog(tk.Toplevel):
def __init__(self, root,personid):
super().__init__(root)
self.personid = personid
self.code = tk.StringVar()
frame1 = ttk.Labelframe(self,text='testo').grid(column=0,row=0,padx=20,pady=20)
ttk.Radiobutton(frame1, text="Option 1", variable=self.code, value="0-0-0").pack()
ttk.Radiobutton(frame1, text="Option 1", variable=self.code, value="0-0-1").pack()
ttk.Radiobutton(frame1, text="Option 1", variable=self.code, value="0-0-2").pack()
self.ok_button = tk.Button(self, text="OK", command=self.on_ok).grid(column=0,row=1)
#self.ok_button.pack()
def on_ok(self, event=None):
self.destroy()
def show(self):
self.wm_deiconify()
self.wait_window()
return self.code.get()
class Example():
def __init__(self, root):
mainframe = ttk.Frame(root).pack(fill="both", expand=True)
self.root = root
ttk.Button(mainframe, text="Get Input", command=self.on_button).pack(padx=8, pady=8)
ttk.Label(mainframe, text="", width=20).pack(side="bottom", fill="both", expand=True)
def on_button(self):
string = PersonEmptyDocsDialog(self.root, 12).show()
print(string)
root = tk.Tk()
root.wm_geometry("400x200")
Example(root)
root.mainloop()

You cannot put in one line. You need to break this into in line 11
frame1 = ttk.Labelframe(self,text='testo')
frame1.grid(column=0,row=0,padx=20,pady=20)
Output:
Output after clicking Get input button:

Related

How do I float tkinter buttons vertically on the left side

I want my tkinter buttons, entry and text to float in the middle vertically, on the left side. I've tried using pack() with side=left, but it just stacked them horizontally, if I could stack them vertically that would be perfect. I've tried using grid, but I can't get them into the middle. I've tried anchor w but that didn't work either. Thanks!
import tkinter as tk
class Fullscreen:
def __init__(self):
self.window = tk.Tk()
self.window.title("Example")
self.fullScreenState = True
self.window.attributes("-fullscreen", self.fullScreenState)
self.w, self.h = self.window.winfo_screenwidth(), self.window.winfo_screenheight()
self.window.geometry("%dx%d" % (self.w, self.h))
self.window.bind("<F11>", self.toggleFullScreen)
self.window.bind("<Escape>", self.quitFullScreen)
def room_button():
room_text = room_key.get()
print(room_text)
def message_button():
message_text = message.get(1.0, tk.END)
handle_text = handle.get()
print(message_text)
print(handle_text)
greeting = tk.Label(text="Welcome")
# Pack displays the Label's text in the window.
greeting.grid(column=1, sticky='w')
display_room = tk.Label(text="Room:")
display_room.grid(column=1, sticky='w')
room_key = tk.Entry()
room_key.grid(column=1, sticky='w')
send_room_key = tk.Button(text="Change Rooms", command=room_button)
send_room_key.grid(column=1, sticky='w')
display_message = tk.Label(text="Message:")
display_message.grid(column=1, sticky='w')
message = tk.Text()
message.grid(column=1, sticky='w')
display_handle = tk.Label(text="Handle (optional):")
display_handle.grid(column=1, sticky='w')
handle = tk.Entry()
handle.grid(column=1, sticky='w')
send_message = tk.Button(text="Send Message", command=message_button)
send_message.grid(column=1, sticky='w')
quit_button = tk.Button(text="Quit", command=self.window.destroy)
quit_button.grid(column=1, sticky='w')
self.window.mainloop()
def toggleFullScreen(self, event):
self.fullScreenState = not self.fullScreenState
self.window.attributes("-fullscreen", self.fullScreenState)
def quitFullScreen(self, event):
self.fullScreenState = False
self.window.attributes("-fullscreen", self.fullScreenState)
if __name__ == '__main__':
app = Fullscreen()
Is this how You want them to be:
from tkinter import Tk, Frame, Label, Entry, Button
root = Tk()
root.geometry('500x400')
widget_frame = Frame(root)
widget_frame.pack(side='left')
Label(widget_frame, text='Enter info below').pack()
entry = Entry(widget_frame)
entry.pack()
Button(widget_frame, text='Submit').pack()
root.mainloop()
You can just use a frame that will be packed to the left and pack widgets inside the frame and they will be on the left side and in the center
You can also use this:
import tkinter as tk
root = tk.Tk()
root.geometry("500x400")
label = tk.Label(root, text="Enter info below")
label.grid(row=1, column=1, pady=(150, 0))
entry = tk.Entry(root)
entry.grid(row=2, column=1)
button = tk.Button(root, text='Submit')
button.grid(row=3, column=1)
root.mainloop()
The pady=(150, 0) tell the grid method that it should leave 150 pixels above the widget empty. This will give you more control over where the widgets are placed.

Why do the buttons not appear in the tkinter window?

I am currently trying to learn tkinter. I do not understand why the buttons I defined do not appear in this code:
from tkinter import *
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
button1 = Button(self, text="Exit", width=12, command=self.clickButton1)
button1.grid(row=0)
button2 = Button(self, text="Test", width=12, command=self.clickButton2)
button2.grid(row=1)
def clickButton1(self):
exit()
def clickButton2(self):
print("Nice")
root = Tk()
app = Window(root)
root.title("Tkinter window")
root.mainloop()
When I don't use a class it works. Like this for example:
from tkinter import *
root = Tk()
button1 = Button(root, text="Works!!!")
button1.grid(row=0)
button2 = Button(root, text="Also works!!!")
button2.grid(row=1)
root.mainloop()
ยดยดยด
The reason is that the class creates a frame, and then puts widgets inside the frame. However, you never add the frame to the window so the widgets inside the frame will be invisible.
You need to make sure and call pack, grid, or place on the instance of Window, just like you do with any other widget.
app = Window(root)
app.pack(fill="both", expand=True)

Create label in tkinter and update the Label with an int variable

I want to create a label and update it with the int-value, which is updated by pressing the buttons, also in the label. I'm still new to Python and would like some help :)
import tkinter as tk
class Main(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.integer = tk.IntVar()
self.integer.set(0)
tk.Button(self, text='Quit', command=self.destroy).pack()
tk.Button(self, text='+', command=self.plus_one).pack()
tk.Button(self, text='-', command=self.take_one).pack()
self.entry0 = tk.Entry(self, textvariable=str(self.integer), justify="center", width=4)
self.entry0.pack()
def plus_one(self):
x = self.integer.get() + 1
self.integer.set(x)
def take_one(self):
x = self.integer.get() - 1
self.integer.set(x)
app = Main()
app.mainloop()
You would do this the same way you did with the Entry widget:
import tkinter as tk
class Main(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.integer = tk.IntVar()
self.integer.set(0)
tk.Button(self, text='Quit', command=self.destroy).pack()
tk.Button(self, text='+', command=self.plus_one).pack()
tk.Button(self, text='-', command=self.take_one).pack()
self.entry0 = tk.Entry(self, textvariable=str(self.integer), justify="center", width=4)
self.entry0.pack()
self.label0 = tk.Label(self, textvariable=str(self.integer))
self.label0.pack()
def plus_one(self):
x = self.integer.get() + 1
self.integer.set(x)
def take_one(self):
x = self.integer.get() - 1
self.integer.set(x)
app = Main()
app.mainloop()
As per your comments, if you are interested in having the binding at button press instead of button release, this has been already addressed here.

Tkinter Reading Input Value from PopUp

Creating a popup window which will ask for email entry and then print the email when 'OK' is pressed, this is my code:
import tkinter as tk
class PopUp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Label(self, text="Main Window").pack()
popup = tk.Toplevel(self)
popup.wm_title("EMAIL")
popup.tkraise(self)
tk.Label(popup, text="Please Enter Email Address").pack(side="left", fill="x", pady=10, padx=10)
self.entry = tk.Entry(popup, bd=5, width=35).pack(side="left", fill="x")
self.button = tk.Button(popup, text="OK", command=self.on_button)
self.button.pack()
def on_button(self):
print(self.entry.get())
app = PopUp()
app.mainloop()
Every time I run it I get this error:
AttributeError: 'NoneType' object has no attribute 'get'
The pop up works how it should but its the input entry that doesn't seem to be working.
I've seen this example before but it wasn't in the popup (I can get it work perfectly without the popup).
Any help is appreciated.
You can store the value in a StringVar variable and get() its value.
import tkinter as tk
from tkinter import StringVar
class PopUp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Label(self, text="Main Window").pack()
popup = tk.Toplevel(self)
popup.wm_title("EMAIL")
popup.tkraise(self)
tk.Label(popup, text="Please Enter Email Address").pack(side="left", fill="x", pady=10, padx=10)
self.mystring = tk.StringVar(popup)
self.entry = tk.Entry(popup,textvariable = self.mystring, bd=5, width=35).pack(side="left", fill="x")
self.button = tk.Button(popup, text="OK", command=self.on_button)
self.button.pack()
def on_button(self):
print(self.mystring.get())
app = PopUp()
app.mainloop()

Problem with tkinter and urllib for checking websites

I am trying to create an application that will allow you to put in multiple websites and check if they are down. When I run the code the window opens and everything works fine until i click search. Then i get the following error
AttributeError: 'Window' object has no attribute 'text_entry'
Any help would be greatly appreciated
#Import everything from tkinter
from tkinter import *
import urllib.request
#Main window
class Window(Frame):
#Master Widget
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
#Creation of 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=BOTH, expand=1)
#Menu
menu = Menu(self.master)
self.master.config(menu=menu)
#File Menu option
file = Menu(menu)
file.add_command(label="Exit", command=self.client_exit)
menu.add_cascade(label="File", menu=file)
#Text Box
text_entry = Entry(self, width=20, bg="white")
text_entry.place(x=0, y=0)
#Submit button
searchButton = Button(self, text='SUBMIT', width=6,
command=self.search)
searchButton.place(x=200, y=30)
#Output Box
output = Text(self, width=20, height=1, bg="white")
output.place(x=0, y=50)
def search(self):
entered_text = self.text_entry.get()
output.delete(0.0, END)
definition=(int(urllib.request.urlopen(entered_text).getcode()))
output.insert(END, definition)
root = Tk()
#Size of the window
root.geometry("400x300")
#Window instance
app = Window(root)
#Show and mainloop
root.mainloop()
You haven't put text_entry on the self object and you can't access it in search function.
#Text Box
self.text_entry = Entry(self, width=20, bg="white")
self.text_entry.place(x=0, y=0)

Categories