用tkinter生成一个界面,界面大小为1080*680,背景为粉色,里面有八个按钮,按钮排列成两排,占屏幕左边一半的位置,分别对应着数字1,2,3,4,5,6,7,8,再生成四个空白框,四个空白框并排并在数字按钮上面,每次点击按钮空白框里面数字会改变,但只能改变一次
时间: 2024-01-13 10:02:22 浏览: 65
下面是一个简单的实现,可以根据需求进行修改:
```python
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.geometry("1080x680")
self.master.title("Button and Label Demo")
self.master.configure(bg='pink')
self.create_widgets()
def create_widgets(self):
self.buttons = []
for i in range(1, 9):
button = tk.Button(self.master, text=str(i), font=('Arial', 24), command=lambda x=i: self.change_label(x))
button.place(x=50 + ((i-1) % 4) * 150, y=50 + ((i-1) // 4) * 100, width=100, height=80)
self.buttons.append(button)
self.labels = []
for i in range(4):
label = tk.Label(self.master, text='', font=('Arial', 32), bg='white')
label.place(x=50 + i * 150, y=250, width=100, height=80)
self.labels.append(label)
def change_label(self, value):
for label in self.labels:
if label['text'] == '':
label.config(text=str(value))
break
root = tk.Tk()
app = Application(master=root)
app.mainloop()
```
运行后会生成一个界面,如下图所示:
![image-20211020170512904](https://i.loli.net/2021/10/20/b8vmj5WU7VcQrqH.png)
每次点击数字按钮,就会把数字填入空白框中,但只能改变一次。
阅读全文