python - 如何隐藏基于单选按钮选择的 tkinter 小部件?
问题描述
我正在尝试使用 tkinter 编写 Python 3 代码,以根据输入的数据生成体积计算。
目前,我的代码有效,但我想做的是当通过单选按钮选择矩形棱镜时,让球体的小部件消失,反之亦然,以便只显示相关项目。
我找到了一些想法,但在我当前的设置中实施它们时遇到了困难。如果有人可以提供帮助,我将非常感激。
代码:
from tkinter import *
from tkinter import messagebox
from volumes import * #basic volume calculations
import sys
def calculate(): #assigns vars, calls volumes module
x = option.get()
if x == 1:
heightx = heighttxt.get()
widthx = widthtxt.get()
lengthx = lengthtxt.get()
height = float(heightx)
width = float(widthx)
length = float(lengthx)
volume= rp_volume(length, width, height)
messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result
if x == 2:
radx = radtxt.get()
radius = float(radx)
volume = sp_volume(radius)
messagebox.showinfo('You selected Sphere', volume) #displays calculation result
window = Tk() #creates window ident
window.title("Volume Calculator")
window.geometry('450x300')
option = IntVar()
option.set(1)
Radiobutton(window, text="Rectangular Prism", variable=option, value=1).grid(column=1, row=1)
Radiobutton(window, text="Sphere", variable=option, value=2).grid(column=2, row=1)
heightlbl = Label(window, text="Enter the height: ", padx=5, pady=5) #creates ident labels
widthlbl = Label(window, text="Enter the width: ", padx=5, pady=5)
lengthlbl = Label(window, text="Enter the length: ", padx=5, pady=5)
radlbl = Label(window, text ="Or enter the radius of a sphere: ", padx=5, pady=5)
heighttxt = Entry(window,width=10) #creates entry boxes
widthtxt = Entry(window,width=10)
lengthtxt = Entry(window,width=10)
radtxt = Entry(window, width=10)
calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!
quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin
heightlbl.grid(column=1, row=3) #assigns grid positions (preferred to pack for precise layout)
widthlbl.grid(column=1, row=4)
lengthlbl.grid(column=1, row=5)
radlbl.grid(column=1, row=6)
heighttxt.grid(column=2, row=3)
widthtxt.grid(column=2, row=4)
lengthtxt.grid(column=2, row=5)
radtxt.grid(column=2, row=6)
calcbtn.grid(column=2, row=7)
quitbtn.grid(column=2, row=8)
window.mainloop() #closes window mainloop
解决方案
To achieve that, you can put the required widgets for the rectangular_prism and the sphere in different frames and add a command parameter to the radio buttons which when selected will show the corresponding the frame and grid_forget
the other. 这是代码。(请注意,我对这个卷模块一无所知,所以你相应地改变它)
from tkinter import *
from tkinter import messagebox
def calculate(): #assigns vars, calls volumes module
x = option.get()
if x == 1:
heightx = heighttxt.get()
widthx = widthtxt.get()
lengthx = lengthtxt.get()
height = float(heightx)
width = float(widthx)
length = float(lengthx)
volume = length*width*height
messagebox.showinfo('You selected Rectangular Prism', volume) #displays calculation result
if x == 2:
radx = radtxt.get()
radius = float(radx)
volume = 4/3*3.14*radius**3
messagebox.showinfo('You selected Sphere', volume) #displays calculation result
def recprism():
sphere_frame.grid_forget()
rec_frame.grid(row=1, column=0)
def sphere():
rec_frame.grid_forget()
sphere_frame.grid(row=1, column=0)
window = Tk() #creates window ident
window.title("Volume Calculator")
window.geometry('450x300')
option = IntVar()
Radiobutton(window, text="Rectangular Prism", variable=option, value=1, command=recprism).grid(column=0, row=0)
Radiobutton(window, text="Sphere", variable=option, value=2, command=sphere).grid(column=1, row=0)
rec_frame = Frame(window)
sphere_frame = Frame(window)
heightlbl = Label(rec_frame, text="Enter the height: ", padx=5, pady=5) #creates ident labels
widthlbl = Label(rec_frame, text="Enter the width: ", padx=5, pady=5)
lengthlbl = Label(rec_frame, text="Enter the length: ", padx=5, pady=5)
radlbl = Label(sphere_frame, text ="Enter the radius of a sphere: ", padx=5, pady=5)
heighttxt = Entry(rec_frame, width=10) #creates entry boxes
widthtxt = Entry(rec_frame, width=10)
lengthtxt = Entry(rec_frame, width=10)
radtxt = Entry(sphere_frame, width=10)
calcbtn = Button(window, text="Calculate the volume", command=calculate, padx=5, pady=5) #hey it's a button that calls the calculate function!
quitbtn = Button(window, text="Quit", command=window.destroy) #quit button does what it says on the tin
heightlbl.grid(column=0, row=0) #assigns grid positions (preferred to pack for precise layout)
widthlbl.grid(column=0, row=1)
lengthlbl.grid(column=0, row=2)
radlbl.grid(column=0, row=0)
heighttxt.grid(column=1, row=0)
widthtxt.grid(column=1, row=1)
lengthtxt.grid(column=1, row=2)
radtxt.grid(column=1, row=0)
calcbtn.grid(column=1, row=1)
quitbtn.grid(column=1, row=2)
window.mainloop()
推荐阅读
- visual-studio-code - Visual Studio Code Installer 有 2 个版本,用户和系统安装程序。如何确定我使用的是哪个版本?
- php - mysqli 是否支持 PHP 7.4 中的 caching_sha2_password?
- gdscript - 枚举和静态类型
- bash - GNU 与自定义脚本并行进行字符串比较
- java - 如何将对象从 ViewHolder 类发送到另一个 Activity
- python - Python - 获取唯一列表
- c++ - 如何在 C++ 中声明一个泛型类
- date - 将 double 类型的变量转换为日期和时间
- c++ - 为 n 维系统实现模块化 Runge-kutta 4 阶方法
- c++ - 现代opengl纹理金字塔