用tkinter创建两级窗口。第一级窗口有5行:第一行为标签一;第二行设三个checkbutton1、2、3;第三行为标签二;第四行设有三个checkbutton4、5、6,第五行为一个确认按钮跳转到二级窗口;第二级窗口有5行:第一行为标签三;第二行根据一级窗口中checkbutton1、2、3的勾选情况对应列出checkbutton11、12、13,checkbutton21、22、23,checkbutton31、32、33;第三行为标签四;第四行根据一级窗口中checkbutton4、5、6的勾选情况对应列出checkbutton41、42、43,checkbutton51、52、53,checkbutton61、62、63;第五行为一个确认按钮print所有二级窗口中checkbutton的布尔值。
时间: 2023-07-04 18:20:57 浏览: 90
好的,以下是使用Tkinter创建两级窗口的代码实现:
```python
import tkinter as tk
class FirstLevelWindow:
def __init__(self):
self.window = tk.Tk()
self.window.title('第一级窗口')
self.label1 = tk.Label(self.window, text='标签一')
self.label1.grid(row=0, column=0, columnspan=3, pady=5)
self.var1 = tk.BooleanVar()
self.var2 = tk.BooleanVar()
self.var3 = tk.BooleanVar()
self.checkbutton1 = tk.Checkbutton(self.window, text='选项一', variable=self.var1)
self.checkbutton1.grid(row=1, column=0)
self.checkbutton2 = tk.Checkbutton(self.window, text='选项二', variable=self.var2)
self.checkbutton2.grid(row=1, column=1)
self.checkbutton3 = tk.Checkbutton(self.window, text='选项三', variable=self.var3)
self.checkbutton3.grid(row=1, column=2)
self.label2 = tk.Label(self.window, text='标签二')
self.label2.grid(row=2, column=0, columnspan=3, pady=5)
self.var4 = tk.BooleanVar()
self.var5 = tk.BooleanVar()
self.var6 = tk.BooleanVar()
self.checkbutton4 = tk.Checkbutton(self.window, text='选项四', variable=self.var4)
self.checkbutton4.grid(row=3, column=0)
self.checkbutton5 = tk.Checkbutton(self.window, text='选项五', variable=self.var5)
self.checkbutton5.grid(row=3, column=1)
self.checkbutton6 = tk.Checkbutton(self.window, text='选项六', variable=self.var6)
self.checkbutton6.grid(row=3, column=2)
self.confirm_button = tk.Button(self.window, text='确认', command=self.open_second_level_window)
self.confirm_button.grid(row=4, column=0, columnspan=3, pady=10)
def open_second_level_window(self):
self.window.destroy()
SecondLevelWindow(self.var1.get(), self.var2.get(), self.var3.get(), self.var4.get(), self.var5.get(), self.var6.get())
def run(self):
self.window.mainloop()
class SecondLevelWindow:
def __init__(self, var1_value, var2_value, var3_value, var4_value, var5_value, var6_value):
self.window = tk.Tk()
self.window.title('第二级窗口')
self.label3 = tk.Label(self.window, text='标签三')
self.label3.grid(row=0, column=0, columnspan=3, pady=5)
self.var11 = tk.BooleanVar(value=var1_value)
self.var12 = tk.BooleanVar(value=var2_value)
self.var13 = tk.BooleanVar(value=var3_value)
self.checkbutton11 = tk.Checkbutton(self.window, text='选项一', variable=self.var11)
self.checkbutton11.grid(row=1, column=0)
self.checkbutton12 = tk.Checkbutton(self.window, text='选项二', variable=self.var12)
self.checkbutton12.grid(row=1, column=1)
self.checkbutton13 = tk.Checkbutton(self.window, text='选项三', variable=self.var13)
self.checkbutton13.grid(row=1, column=2)
self.label4 = tk.Label(self.window, text='标签四')
self.label4.grid(row=2, column=0, columnspan=3, pady=5)
self.var41 = tk.BooleanVar(value=var4_value)
self.var42 = tk.BooleanVar(value=var5_value)
self.var43 = tk.BooleanVar(value=var6_value)
self.checkbutton41 = tk.Checkbutton(self.window, text='选项四', variable=self.var41)
self.checkbutton41.grid(row=3, column=0)
self.checkbutton42 = tk.Checkbutton(self.window, text='选项五', variable=self.var42)
self.checkbutton42.grid(row=3, column=1)
self.checkbutton43 = tk.Checkbutton(self.window, text='选项六', variable=self.var43)
self.checkbutton43.grid(row=3, column=2)
self.confirm_button = tk.Button(self.window, text='确认', command=self.print_checkbutton_values)
self.confirm_button.grid(row=4, column=0, columnspan=3, pady=10)
def print_checkbutton_values(self):
print(f'选项一:{self.var11.get()}')
print(f'选项二:{self.var12.get()}')
print(f'选项三:{self.var13.get()}')
print(f'选项四:{self.var41.get()}')
print(f'选项五:{self.var42.get()}')
print(f'选项六:{self.var43.get()}')
def run(self):
self.window.mainloop()
if __name__ == '__main__':
first_level_window = FirstLevelWindow()
first_level_window.run()
```
这个程序实现了要求的功能,第一级窗口中有5行,第二级窗口中有5行,并且第二级窗口的checkbutton根据第一级窗口中的勾选情况动态生成。当第一级窗口中的确认按钮被点击时,程序会关闭第一级窗口,打开第二级窗口;当第二级窗口中的确认按钮被点击时,程序会打印所有checkbutton的布尔值。
阅读全文