TTk radio button - python

I have defined a class 'exemple' where i did two radio buttons , when i press 'bu1 ' spangender dont take any value.help me please
from tkinter import *
from tkinter import ttk
class exemple():
def like(self):
root = Tk()
style = ttk.Style()
style.theme_use('classic')
spangender = StringVar()
rb1 = ttk.Radiobutton(root, text='male', variable=spangender, value='male')
rb1.grid(column=0, row=0)
rb2 = ttk.Radiobutton(root, text='female', variable=spangender, value='female')
rb2.grid(column=1, row=0)
bu1 = ttk.Button(root, text='ok', command=lambda: get())
bu1.grid(column=2, row=1)
def get():
print(spangender.get())#Dosen't work
root.mainloop()
root = Tk()
style = ttk.Style()
style.theme_use('classic')
def get():
ab = exemple()
ab.like()
bu1 = ttk.Button(root, text='ok', command=lambda: get())
bu1.grid(column=2, row=1)
root.mainloop()

Related

Get the output label of calender toplevel from the master window having "Select Date" button, tkinter

I have a doubt, how to get the top-level value in the master window's label. I mean like i couldn't return the entered value from toplevel window to main's self.
I tried other ways and it didn't work
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkcalendar import Calendar, DateEntry
class Application(tk.Frame):
def _init_(self,master):
Frame._init_(self,master)
self.master=master
self.label=Label(text="",font=("Georgia,12"))
self.label.place(x=50,y=80)
self.create_widgets()
def calendar_view():
def print_sel():
return cal.get()
top = tk.Toplevel(root)
cal = Calendar(top,
font="Arial 14",selectmode='day',
cursor="hand1", year=2020)
cal.pack(fill="both", expand=True)
ttk.Button(top, text="ok", command=print_sel).pack()
root=tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')
root.title("Date")
root.geometry("800x500")
button = tk.Button(root, text = 'Test Start Date', bg='#0073BD',height=1,width=15,padx=5,pady=2, fg="white",command=calendar_view)
button.place(x=500,y=100)
label1 = tk.Label(root, text="<-Click to select Test Start Date", padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
label1.place(x=630,y=100)
button = tk.Button(root, text = 'Test End Date', bg='#0073BD',height=1,width=15,padx=5,pady=2,fg="white",command=calendar_view)
button.place(x=500,y=150)
label1 = tk.Label(root, text="<-Click to select Test End Date", padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
label1.place(x=630,y=150)
app=Application(root)
root.configure(bg='#e6e9f2')
root.mainloop()
[here is the reference of the image]1
Suggest to use two StringVar to store the start date and end date, and pass the variable to calendar_view():
def calendar_view(var):
def print_sel():
# update the var
var.set(cal.get_date())
# close the calendar window
top.destroy()
top = tk.Toplevel(root)
cal = Calendar(top, font="Arial 14",selectmode='day',
cursor="hand1", year=2020, date_pattern='y-mm-dd')
cal.pack(fill="both", expand=True)
ttk.Button(top, text="ok", command=print_sel).pack()
# make window a modal window
top.grab_set()
top.wait_window(top)
Then update the creation of the two buttons:
button = tk.Button(root, text = 'Test Start Date', bg='#0073BD',height=1,width=15,padx=5,pady=2, fg="white",
command=lambda: calendar_view(startdate))
button = tk.Button(root, text = 'Test End Date', bg='#0073BD',height=1,width=15,padx=5,pady=2,fg="white",
command=lambda: calendar_view(enddate))
and associate the two variables to the two labels, so that their text are updated whenever the variables are updated:
startdate = tk.StringVar(value="<-Click to select Test Start Date")
label1 = tk.Label(root, textvariable=startdate, padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
enddate = tk.StringVar(value="<-Click to select Test End Date")
label2 = tk.Label(root, textvariable=enddate, padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
#acw1668 Here is the updated code, can you send me your version updated code
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkcalendar import Calendar, DateEntry
class Application(tk.Frame):
def _init_(self,master):
Frame._init_(self,master)
self.master=master
self.label=Label(text="",font=("Georgia,12"))
self.label.place(x=50,y=80)
self.create_widgets()
caldate1 = " "
caldate2 = " "
def calendar_view1():
def print_sel1():
global caldate1
caldate1 = cal1.selection_get()
label1.config(text = caldate1)
return caldate1
top1 = tk.Toplevel(root)
cal1 = Calendar(top1,
font="Arial 14",selectmode='day',
cursor="hand2", year=2020)
cal1.pack(fill="both", expand=True)
ttk.Button(top1, text="ok", command=print_sel1).pack()
def calendar_view2():
def print_sel2():
global caldate2
caldate2 = cal2.selection_get()
label2.config(text = caldate2)
return caldate2
top2 = tk.Toplevel(root)
cal2 = Calendar(top2,
font="Arial 14",selectmode='day',
cursor="hand2", year=2020)
cal2.pack(fill="both", expand=True)
ttk.Button(top2, text="ok", command=print_sel2).pack()
root=tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')
root.title("Date")
root.geometry("800x500")
button = tk.Button(root, text = 'Test Start Date', bg='#0073BD',height=1,width=15,padx=5,pady=2, fg="white",command=calendar_view1)
button.place(x=500,y=100)
label1 = tk.Label(root, text="<-Click to select Test Start Date", padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
label1.place(x=630,y=100)
button = tk.Button(root, text = 'Test End Date', bg='#0073BD',height=1,width=15,padx=5,pady=2,fg="white",command=calendar_view2)
button.place(x=500,y=150)
label2 = tk.Label(root, text="<-Click to select Test End Date", padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
label2.place(x=630,y=150)
app=Application(root)
root.configure(bg='#e6e9f2')
root.mainloop()

why is .grid giving me an error when I am trying to move my button

import time
import tkinter as tk
from tkinter import scrolledtext
win = tk.Tk()
win.title("My First Game")
win.configure(bg="black")
win.geometry("640x400")
label = tk.Label(win, text="test", fg="red", bg="black").pack()
canvas1 = tk.Canvas(win, width=130, height=20)
canvas1.pack()
entry1 = tk.Entry(win, font="Helvetica 10")
canvas1.create_window(65, 10, window=entry1)
entry1.insert(0, "Type here")
def shortcut():
Shortcut = tk.Label(win, fg="red", bg="black", text="test2")
Shortcut.pack()
button1 = tk.Button(win, text="Enter", fg="red", bg="black", command=shortcut)
button1.pack()
exit_button = tk.Button(win, text="Quit", padx=4, pady=2, bg="black", fg="red", command=quit)
exit_button.pack()
exit_button.grid(row=0, column=2)
win.mainloop()
Why is this giving me an error? I tried in a separate project with just a black screen and the button and it worked fine. But when I put it in the code above it doesn't work
line 42, in <module> exit_button.grid(row=0, column=2)
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
As #10Rep mentioned in comment - you can remove grid() to resolve problem with error.
import tkinter as tk
# --- functions ---
def shortcut():
shortcut = tk.Label(win, text="test2")
shortcut.pack()
# --- main ---
win = tk.Tk()
label = tk.Label(win, text="test")
label.pack()
canvas1 = tk.Canvas(win) #, width=130, height=20)
canvas1.pack()
entry1 = tk.Entry(canvas1)
canvas1.create_window(0, 0, window=entry1, anchor='nw')
entry1.insert(0, "Type here")
button1 = tk.Button(win, text="Enter", command=shortcut)
button1.pack()
exit_button = tk.Button(win, text="Quit", command=win.destroy)
exit_button.pack()
win.mainloop()
But I expect that you used grid() to organize two buttons in one line.
Problem is that you can't mix pack() and grid() in one window/frame and I see two solutions:
First is to use only grid() to organize all widgets
import tkinter as tk
# --- functions ---
def shortcut():
shortcut = tk.Label(win, text="test2")
shortcut.grid(row=3, column=0, columnspan=2)
# --- main ---
win = tk.Tk()
label = tk.Label(win, text="test")
label.grid(row=0, column=0, columnspan=2)
canvas1 = tk.Canvas(win) #, width=130, height=20)
canvas1.grid(row=1, column=0, columnspan=2)
entry1 = tk.Entry(canvas1)
canvas1.create_window(0, 0, window=entry1, anchor='nw')
entry1.insert(0, "Type here")
button1 = tk.Button(win, text="Enter", command=shortcut)
button1.grid(row=2, column=0)
exit_button = tk.Button(win, text="Quit", command=win.destroy)
exit_button.grid(row=2, column=1)
win.mainloop()
Second is to put Frame (using pack()) and put buttons inside this frame using grid()
import tkinter as tk
# --- functions ---
def shortcut():
shortcut = tk.Label(win, text="test2")
shortcut.pack()
# --- main ---
win = tk.Tk()
label = tk.Label(win, text="test")
label.pack()
canvas1 = tk.Canvas(win) #, width=130, height=20)
canvas1.pack()
entry1 = tk.Entry(canvas1)
canvas1.create_window(0, 0, window=entry1, anchor='nw')
entry1.insert(0, "Type here")
# - frame with grid -
f = tk.Frame(win)
f.pack()
button1 = tk.Button(f, text="Enter", command=shortcut)
button1.grid(row=0, column=0)
exit_button = tk.Button(f, text="Quit", command=win.destroy)
exit_button.grid(row=0, column=1)
# -
win.mainloop()
or using pack(side=...)
import tkinter as tk
# --- functions ---
def shortcut():
shortcut = tk.Label(win, text="test2")
shortcut.pack()
# --- main ---
win = tk.Tk()
label = tk.Label(win, text="test")
label.pack()
canvas1 = tk.Canvas(win) #, width=130, height=20)
canvas1.pack()
entry1 = tk.Entry(canvas1)
canvas1.create_window(0, 0, window=entry1, anchor='nw')
entry1.insert(0, "Type here")
# - frame with pack(side=...) -
f = tk.Frame(win)
f.pack()
button1 = tk.Button(f, text="Enter", command=shortcut)
button1.pack(side='left')
exit_button = tk.Button(f, text="Quit", command=win.destroy)
exit_button.pack(side='left')
# -
win.mainloop()

How do I use Tkinter buttons to open webrowsers properly?

Everything looks right, but when I run the program the buttons and the websites open at the same time and then the buttons don't work?
import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
style = ttk.Style()
open_facebook = webbrowser.open('http://www.facebook.com')
open_google = webbrowser.open('http://www.google.com')
open_yahoo = webbrowser.open('http://www.yahoo.com')
open_youtube = webbrowser.open('http://www.youtube.com')
style.configure("TButton",
font="Serif 18",
padding=10)
main_frame = Frame(root)
main_frame.grid(row=0, columnspan=4)
button_facebook = ttk.Button(main_frame, text='Facebook', command=open_facebook).grid(row=1, column=0)
button_google = ttk.Button(main_frame, text='Google', command=open_google).grid(row=1, column=1)
button_yahoo = ttk.Button(main_frame, text='Yahoo', command=open_yahoo).grid(row=1, column=2)
button_youtube = ttk.Button(main_frame, text='Youtube', command=open_youtube).grid(row=1, column=3)
root.mainloop()
I couldn't manage to make it work with the code you had presented, but I could make it work using lambda in the command portion of the button. This was the only way I could ensure that the web browser didn't open the sites until the buttons were pressed.
import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
style = ttk.Style()
style.configure("TButton",
font="Serif 18",
padding=10)
main_frame = Frame(root)
main_frame.grid(row=0, columnspan=4)
button_facebook = ttk.Button(main_frame, text='Facebook', command= lambda:
webbrowser.open('http://www.facebook.com'))
button_google = ttk.Button(main_frame, text='Google', command= lambda:
webbrowser.open('http://www.google.com'))
button_yahoo = ttk.Button(main_frame, text='Yahoo', command= lambda:
webbrowser.open('http://www.yahoo.com'))
button_youtube = ttk.Button(main_frame, text='Youtube', command= lambda:
webbrowser.open('http://www.youtube.com'))
button_facebook.grid(row=1, column=0)
button_google.grid(row=1, column=1)
button_yahoo.grid(row=1, column=2)
button_youtube.grid(row=1, column=3)
root.mainloop()

How to design GUI properly

I'm new to python and I don't understand how do I configure the resolution of the font, I wanted to design it but it keeps on expanding, because of the grid(?), Thanks in advance
Here's my code:
import tkinter
from tkinter import ttk
selection=('Weight','Volume','Length')
length = ('mm', 'cm', 'inches','feet', 'yards', 'meter', 'km', 'miles')
weight=('mg','lb','g','kg','oz','ton')
volume=('ml','l','gal','pt','qt')
#title
window = tkinter.Tk()
window.title("Conversion APP")
window.configure(background='#a1dbcd')
title=tkinter.Label(window,text='Welcome to the Converter APP!', fg="#383a39", bg="#a1dbcd", font=("Helvetica",16))
title.grid(row=0,column=2)
title.config(width=100)
window.geometry("300x200")
#1st
labelOne = ttk.Label(window,text='Enter Value',background='blue')
labelOne.grid(row=1,column=0)
to_be_converted = ttk.Combobox(values=length, width=10)
to_be_converted.grid(row=1, column=2)
#2nd
labelTwo = ttk.Label(window, text="Convert To",background='blue')
labelTwo.grid(row=2,column=0)
converted = ttk.Combobox(values=length, width=10)
converted.grid(row=2, column=2)
#input
userName = tkinter.DoubleVar()
userEntry = ttk.Entry(window, width=5, textvariable = userName)
userEntry.grid(row=1, column=1)
#button to convert
btn = ttk.Button(window, text='Convert!', command=convert_length or convert_weight or convert_volume)
btn.grid(row=1, column=4)
#button to exit
exit= ttk.Button(window, text='Quit', command=exit_converter)
exit.grid(row=2, column=4)
window.mainloop()

AttributeError in Tkinter while working with control variables in radiobuttons

I'm trying to get the value of the radiobutton selcted and storing this int into a varable. It's my first tkinter project, so I'm sorry for my probably stupid mistakes...
from tkinter import *
from tkinter import ttk
select = "A"
def begin():
grand = Tk()
grand.title("Converter")
window.destroy()
frame = Frame(grand)
option = IntVar()
-> AttributeError: 'NoneType' object has no attribute '_root'
grandlabel = Label(frame, text="Choose the grand").grid(row=0, sticky=N, padx=5)
grand1 = Radiobutton(frame, text="Speed", variable=option, value=1, command=sel).grid(row=1, sticky=W)
grand2 = Radiobutton(frame, text="etc", variable=option, value=2, command=sel).grid(row=2, sticky=W)
submitgrand = Button(frame, text="Ok", command=unit).grid(row=3, sticky=W)
frame.pack()
grand.mainloop()
def sel():
global option
global select
select = option.get()
option = StringVar()
def unit():
unit = Tk()
global select
select = grandchosen
if (grandchosen == "Speed"):
Label(unit, text="Test").pack()
else:
Label(unit, text="Test2").pack()
unit.mainloop()
root = Tk()
frame = Frame(root)
welcome = ttk.Label(frame, text="Welcome!").grid(row=0, sticky=N, padx=10, pady=3)
okbutton = Button(frame, text="Ok", width=15, command=begin).grid(row=1, sticky=S, padx=20, pady=30)
frame.pack()
style = ttk.Style()
style.configure("TLabel", foreground="midnight blue", font="Times 19")
root.mainloop()
Would be great to get some help, thank you!

Categories