I have a Python file with 50+ frames. Each frame has 5 radio buttons and 1 textbox. Each radio button has a pre-determine numeric value that will print to the textbox.
What I would like to do is replace the radio buttons with a combobox. the combobox is set up with a base numeric value with a math equation.
The principle works but I can only get it to print to shell.
I have tried numerous different codes from posts for the past months.
I thought the community may be able to help me.
I have attached a snippet of my code.
from tkinter import *
from tkinter import Tk
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import Combobox
root=tk.Tk()
root.title("Dental Milling Machines")
root.geometry("250x200")
def onclick1():
textbox1.delete('1.0', 'end')
textbox1.insert('end', '2.83')
def onclick2():
textbox1.delete('1.0', 'end')
textbox1.insert('end', '5.66')
def Cnum():
print(combobox1.current()*2.83)
cb_var1 = tk.IntVar()
frame1 = Frame(root, height = 150, width= 150, relief= RAISED, bd=8, bg="blue")
frame1.grid(row=0, column=0, pady=2,sticky="NW")
label = Label(frame1, text="Frame 1", fg="red")
label.grid(row=0, columnspan=3, pady= 1, sticky= "W")
button1=Radiobutton(frame1, text="Submit", command=Cnum)
button1.grid(row=1, column=1, pady= 1, padx= 5, sticky= "W")
textbox1=Text(frame1, borderwidth=1, wrap="none", width=5, height=1)
textbox1.grid(row=0, column=1,padx=10, sticky="W")
combobox1=Combobox(frame1, width=7)
combobox1.grid(row=1, column=0)
combobox1['values'] = ( '', ' 1', ' 2', ' 3', ' 4', ' 5')
button1=Radiobutton(frame1, text="1 Unit ", variable=cb_var1, command=onclick1)
button1.grid(row=2, column=0, pady= 1, padx= 5, sticky= "W")
button2=Radiobutton(frame1, text="2 Unit ", variable=cb_var1, command=onclick2)
button2.grid(row=4, column=0, pady= 1, padx= 5, sticky= "W")
root.mainloop()
This will put the result of 2.83 multiplied by the selected value from the combobox in textbox1.
def Cnum():
textbox1.insert(tk.END, combobox1.current() * 2.83)
If you want the value in the textbox to be updated when a value is selected in the combobox we can bind it's ComboboxSelected event to Cnum.
For that to work we need to change a couple of things in the code.
First we need to add code to bind the event,
combobox1=Combobox(frame1, width=7)
combobox1.grid(row=1, column=0)
combobox1['values'] = ( '', ' 1', ' 2', ' 3', ' 4', ' 5')
# bind ComboboxSelected event to Cnum
combobox1.bind('<<ComboboxSelected>>', Cnum)
and we need to alter the Cnum function to take the event argument.
def Cnum(event):
textbox1.delete('1.0', 'end')
textbox1.insert("1.0", combobox1.current() * 2.83)
Now to set/change the value in the textbox all you need to do is select a value in the combobox.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.