python寫一個GUI窗口,窗口中有菜單欄,菜單欄中有多個子菜單,每個子菜單中在窗口顯示是標簽和文本框内容和位置都不一致
时间: 2023-11-09 22:06:41 浏览: 27
可以使用Python自带的Tkinter库来实现GUI窗口,具体代码如下:
```python
import tkinter as tk
class MyGUI:
def __init__(self, master):
self.master = master
master.title("My GUI")
# 创建菜单栏
menubar = tk.Menu(master)
master.config(menu=menubar)
# 创建子菜单1
submenu1 = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="Menu1", menu=submenu1)
submenu1.add_command(label="Submenu1-1", command=self.show_label1)
submenu1.add_command(label="Submenu1-2", command=self.show_label2)
# 创建子菜单2
submenu2 = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="Menu2", menu=submenu2)
submenu2.add_command(label="Submenu2-1", command=self.show_text1)
submenu2.add_command(label="Submenu2-2", command=self.show_text2)
# 创建标签和文本框
self.label1 = tk.Label(master, text="Label 1")
self.label2 = tk.Label(master, text="Label 2")
self.text1 = tk.Text(master, height=5, width=30)
self.text2 = tk.Text(master, height=10, width=50)
def show_label1(self):
self.label1.grid(row=0, column=0)
def show_label2(self):
self.label2.grid(row=1, column=0)
def show_text1(self):
self.text1.grid(row=2, column=0)
def show_text2(self):
self.text2.grid(row=3, column=0)
root = tk.Tk()
my_gui = MyGUI(root)
root.mainloop()
```
在这个GUI窗口中,我们创建了一个菜单栏,并在菜单栏中创建了两个子菜单。每个子菜单都有两个选项,点击选项后会在窗口中显示一个标签或一个文本框。标签和文本框的位置可以通过grid函数来指定。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)