【发布时间】:2019-10-09 16:58:58
【问题描述】:
我正在尝试做一个简单的程序。我正在尝试检查单选按钮 1 是否被选中显示一个按钮,如果单选按钮 2 被选中并且屏幕上是否有一个按钮消失。请帮帮我。
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
bati = tkinter.Tk()
bati.geometry('500x500')
bati.title('Project')
def hello():
messagebox.showinfo("Say Hello", "Hello World")
def askfile():
bati.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
lb2 = Label(bati, text=bati.filename, fg='red', font=("Times", 10, "bold"))
lb2.place(x='270',y='34')
def first():
b = Button(bati, text='Import', activebackground='red', bd='3', bg='gray', fg='yellow', font=("Times New Roman", 10, "bold"), command=askfile)
b.place(x='200',y='30')
working = True
def second():
if working == True:
b.widget.pack_forget()
working = False
canvas_width = 3000
canvas_height = 220
w = Canvas(bati, width=canvas_width,height=canvas_height)
w.pack()
y = int(canvas_height / 2)
w.create_line(0, y, canvas_width, y, fill="#476042", width='2')
v = IntVar()
v.set('L')
rb1 = Radiobutton(bati, text='Import A File', value=1, variable=v, command=first, font=("Comic Sans MS", 10, "bold"))
rb2 = Radiobutton(bati, text='Choose From Web', value=2, variable=v, command=second, font=("Comic Sans MS", 10, "bold"))
rb1.place(x='50',y='30')
rb2.place(x='50',y='50')
working = False
bati.mainloop()
【问题讨论】:
-
b和working当前是first()中的局部变量,并在该函数退出时消失。second()不可能访问它们。 -
那我需要做什么?
-
提示:为什么
bati.filename在askfile内部工作?您处理bati的方式与处理b的方式有什么不同?具体来说:代码在哪里bati得到它的初始值?是在函数内部还是外部?
标签: python python-3.x tkinter tk