单击单选按钮 Tkinter 后使框架变灰 [英] Gray out a frame after clicking a radiobutton Tkinter

查看:24
本文介绍了单击单选按钮 Tkinter 后使框架变灰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,我试图在用户单击其中一个选项时使第一帧变灰,以便用户无法更改为其他选项.但是当我运行它时,它甚至在用户选择之前就已经使框架变灰了.我还想让第二帧变灰,直到用户单击/选择第一帧.有人可以帮忙吗?

i have this code, i'm trying to make the the first frame to gray out once the user click one of the choices so that the user cannot change to other choice. but when i run it, it already gray out the frame even before the user choose. i also want to make the second frame to be gray out until the user click/choose fro the first frame. can anyone help?

  from Tkinter import *
  def onclick():
     pass

  import tkMessageBox
  import ttk 

  root = Tk()

  root.title("Pantai Hospital")
  root.geometry("200x200")

  Label(root, text = "Welcome to Pantai Hospital!").grid()


  f1 = Frame(root, width = 350, height = 110) 

  f2 = Frame(f1, relief = GROOVE, borderwidth = 2)

  l9 = Label(f2, text = "Choose your specialist:")
  l9.grid()

  specialistchoose = IntVar()

  r1 = Radiobutton (f2, text = "Cardiology", variable = specialistchoose, value = 1)
  r1.grid(row = 1, column = 0, stick = W) 

  r2 = Radiobutton (f2, text = "Gastroenterology", variable = specialistchoose, value = 2)
  r2.grid(row = 1, column = 1,stick = W ) 

  r3 = Radiobutton (f2, text = "Dermatology", variable = specialistchoose, value = 3)
  r3.grid (row = 1, column = 2,stick = W )

  r4 = Radiobutton (f2, text = "Psychiatry", variable = specialistchoose, value = 4)
  r4.grid (row = 3, column = 0,stick = W )

  r5 = Radiobutton (f2, text = "Dentist", variable = specialistchoose, value = 5)
  r5.grid(row = 3, column = 1,stick = W  )

  f2.place(relx = 0.01, rely = 0.125, anchor = NW)
  Label(f1, text = "Specialist").place(relx = .06, rely = 0.125, anchor = W)

  f1.grid()

  if specialistchoose.get() <> 'Cardiology' or 'Gastroenterology' or 'Dermatology' or 'Psychiatry' or 'Dentist' :
  for child in f2.winfo_children():
      child.configure(state = 'disable')

  f3 = Frame(root, width = 350, height = 110)
  f4 = Frame(f3, relief = GROOVE, borderwidth = 2)

  l15 = Label (f4, text = "Choose your preferred day:")
  l15.grid(columnspan = 2, sticky = W) 

  day = IntVar()

  cb1 = Checkbutton(f4, text = "Monday", variable = day, onvalue = 1, offvalue = 0)
  cb1.grid(row = 4, column = 0,stick = W ) 
  cb2 = Checkbutton(f4, text = "Tuesday", variable = day, onvalue = 2, offvalue = 0)
  cb2.grid(row = 4, column = 1,stick = W ) 
  cb3 = Checkbutton(f4, text = "Wednesday", variable = day, onvalue = 3, offvalue = 0)
  cb3.grid(row = 4, column = 2, stick = W) 
  cb4 = Checkbutton(f4, text = "Thursday", variable = day, onvalue = 4, offvalue = 0)
  cb4.grid(row = 4, column = 3,stick = W ) 
  cb5 = Checkbutton (f4, text = "Friday", variable = day, onvalue = 5, offvalue = 0)
  cb5.grid(row = 5, column = 0, stick = W )
  cb6 = Checkbutton(f4, text = "Saturday", variable = day, onvalue = 6, offvalue = 0)
  cb6.grid(row = 5, column = 1, stick = W )
  cb7 = Checkbutton (f4, text = "Sunday", variable = day, onvalue = 7, offvalue = 0)
  cb7.grid(row = 5, column = 2, stick = W ) 

  f4.place(relx = 0.01, rely = 0.125, anchor = NW)
  Label(f3, text = "Day").place(relx = .06, rely = 0.125, anchor = W)

  f3.grid()
  root.mainloop() 

推荐答案

开始时specialistchoose.get()的值为0,检查带有 if 的按钮不是动态的.

At the beginning the value of specialistchoose.get() is 0, and the check for the buttons with an if is not dynamic.

Radiobutton 内置了 command,每次按下按钮时都会对其进行评估.您可以使用它来停用按钮.

The Radiobutton has a build in command which is evaluated every time the button is pressed. You can use that to deactivate the buttons.

像这样:

from Tkinter import *

def func():
    for child in f2.winfo_children():
        child.configure(state = 'disable')

import tkMessageBox
import ttk

root = Tk()

root.title("Pantai Hospital")
root.geometry("200x200")

Label(root, text = "Welcome to Pantai Hospital!").grid()


f1 = Frame(root, width = 350, height = 110)

f2 = Frame(f1, relief = GROOVE, borderwidth = 2)

l9 = Label(f2, text = "Choose your specialist:")
l9.grid()

specialistchoose = IntVar()

r1 = Radiobutton (f2, text = "Cardiology", variable = specialistchoose, value = 1, command=func)
r1.grid(row = 1, column = 0, stick = W)

r2 = Radiobutton (f2, text = "Gastroenterology", variable = specialistchoose, value = 2, command=func)
r2.grid(row = 1, column = 1,stick = W )

r3 = Radiobutton (f2, text = "Dermatology", variable = specialistchoose, value = 3, command=func)
r3.grid (row = 1, column = 2,stick = W )

r4 = Radiobutton (f2, text = "Psychiatry", variable = specialistchoose, value = 4, command=func)
r4.grid (row = 3, column = 0,stick = W )

r5 = Radiobutton (f2, text = "Dentist", variable = specialistchoose, value = 5, command=func)
r5.grid(row = 3, column = 1,stick = W  )

f2.place(relx = 0.01, rely = 0.125, anchor = NW)
Label(f1, text = "Specialist").place(relx = .06, rely = 0.125, anchor = W)

f1.grid()



f3 = Frame(root, width = 350, height = 110)
f4 = Frame(f3, relief = GROOVE, borderwidth = 2)

l15 = Label (f4, text = "Choose your preferred day:")
l15.grid(columnspan = 2, sticky = W)

day = IntVar()

cb1 = Checkbutton(f4, text = "Monday", variable = day, onvalue = 1, offvalue = 0)
cb1.grid(row = 4, column = 0,stick = W )
cb2 = Checkbutton(f4, text = "Tuesday", variable = day, onvalue = 2, offvalue = 0)
cb2.grid(row = 4, column = 1,stick = W )
cb3 = Checkbutton(f4, text = "Wednesday", variable = day, onvalue = 3, offvalue = 0)
cb3.grid(row = 4, column = 2, stick = W)
cb4 = Checkbutton(f4, text = "Thursday", variable = day, onvalue = 4, offvalue = 0)
cb4.grid(row = 4, column = 3,stick = W )
cb5 = Checkbutton (f4, text = "Friday", variable = day, onvalue = 5, offvalue = 0)
cb5.grid(row = 5, column = 0, stick = W )
cb6 = Checkbutton(f4, text = "Saturday", variable = day, onvalue = 6, offvalue = 0)
cb6.grid(row = 5, column = 1, stick = W )
cb7 = Checkbutton (f4, text = "Sunday", variable = day, onvalue = 7, offvalue = 0)
cb7.grid(row = 5, column = 2, stick = W )

f4.place(relx = 0.01, rely = 0.125, anchor = NW)
Label(f3, text = "Day").place(relx = .06, rely = 0.125, anchor = W)

f3.grid()
root.mainloop()

代码旁注:

在这种情况下,代码中的 if 语句总是计算 True,因为 'Cardiology' 或 'Gastroenterology'True

The if statement in your code always evaluates True in that case, as 'Cardiology' or 'Gastroenterology' is True

你应该使用:

if specialistchoose.get() != 'Cardiology' or specialistchoose.get() != 'Gastroenterology' or specialistchoose.get() != 'Dermatology' or specialistchoose.get() != 'Psychiatry' or specialistchoose.get() != 'Dentist':

这篇关于单击单选按钮 Tkinter 后使框架变灰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆