用tinker库比较两个数字的大小
时间: 2024-05-11 07:14:56 浏览: 115
两个数的大小比较
以下是使用tinker库比较两个数字大小的Python代码示例:
```python
from tkinter import *
def compare_numbers():
num1 = int(entry1.get())
num2 = int(entry2.get())
if num1 > num2:
result_label.config(text="Number 1 is greater than Number 2")
elif num1 < num2:
result_label.config(text="Number 2 is greater than Number 1")
else:
result_label.config(text="Both numbers are equal")
# Create a Tkinter window
window = Tk()
window.title("Number Comparison")
# Create input fields for two numbers
label1 = Label(window, text="Enter Number 1:")
label1.pack()
entry1 = Entry(window)
entry1.pack()
label2 = Label(window, text="Enter Number 2:")
label2.pack()
entry2 = Entry(window)
entry2.pack()
# Create a button to compare the numbers
compare_button = Button(window, text="Compare", command=compare_numbers)
compare_button.pack()
# Create a label to display the result
result_label = Label(window, text="")
result_label.pack()
# Run the GUI window
window.mainloop()
```
这个程序创建了一个GUI窗口,其中包含两个输入框和一个按钮。当用户单击按钮时,程序将提取两个数字并比较它们。然后,程序将在标签上显示比较结果。
阅读全文