使用tkinter实现两个数比大小
时间: 2024-05-09 16:21:26 浏览: 75
以下是使用tkinter实现两个数比大小的Python代码示例:
```python
import tkinter as tk
def compare_numbers():
num1 = int(entry1.get())
num2 = int(entry2.get())
if num1 > num2:
result_label.config(text="Number 1 is greater.")
elif num2 > num1:
result_label.config(text="Number 2 is greater.")
else:
result_label.config(text="Both numbers are equal.")
root = tk.Tk()
root.geometry("300x200")
root.title("Compare Numbers")
num1_label = tk.Label(root, text="Number 1:")
num1_label.pack()
entry1 = tk.Entry(root)
entry1.pack()
num2_label = tk.Label(root, text="Number 2:")
num2_label.pack()
entry2 = tk.Entry(root)
entry2.pack()
compare_button = tk.Button(root, text="Compare", command=compare_numbers)
compare_button.pack()
result_label = tk.Label(root, text="")
result_label.pack()
root.mainloop()
```
这个程序创建了一个简单的GUI界面,其中有两个输入框用于输入数字,一个“比较”按钮用于比较这两个数字,一个标签用于显示比较结果。当用户点击“比较”按钮时,程序将获取两个输入框中的数字,并将它们与彼此进行比较。比较结果将显示在标签中。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)