python编制求两个正整数的最小公倍数的程序。(1)两个文本框 txt、txt2,用来输入整型数 据;(2)一个按钮;一个不可编辑的文本组件 txt3。当单击按钮时,在 txt3 中显示两个整数 的最小公倍数。
时间: 2024-03-15 20:44:32 浏览: 71
好的,以下是一个 Python 编写的求两个正整数最小公倍数的程序,使用了 tkinter 库来创建 GUI 界面:
```
import tkinter as tk
# 计算最小公倍数
def lcm(a, b):
m = a * b
while b:
a, b = b, a % b
return m // a
# 创建窗口和控件
window = tk.Tk()
window.geometry('300x150')
window.title('最小公倍数计算器')
tk.Label(window, text='请输入两个正整数:').pack()
frame = tk.Frame(window)
frame.pack()
tk.Label(frame, text='数1:').pack(side=tk.LEFT)
txt1 = tk.Entry(frame, width=10)
txt1.pack(side=tk.LEFT)
tk.Label(frame, text='数2:').pack(side=tk.LEFT)
txt2 = tk.Entry(frame, width=10)
txt2.pack(side=tk.LEFT)
def calculate_lcm():
try:
num1 = int(txt1.get())
num2 = int(txt2.get())
res = lcm(num1, num2)
txt3.config(state='normal')
txt3.delete(0, tk.END)
txt3.insert(0, str(res))
txt3.config(state='readonly')
except ValueError:
txt3.config(state='normal')
txt3.delete(0, tk.END)
txt3.insert(0, '请输入整数!')
txt3.config(state='readonly')
tk.Button(window, text='计算最小公倍数', command=calculate_lcm).pack()
txt3 = tk.Entry(window, width=20, state='readonly')
txt3.pack(pady=10)
window.mainloop()
```
在上述代码中,`lcm` 函数用于计算两个正整数的最小公倍数。在 GUI 界面中,首先创建了一个窗口和需要的控件,包括两个文本框 `txt1` 和 `txt2`,一个按钮,以及一个不可编辑的文本组件 `txt3`。当单击按钮时,调用 `calculate_lcm` 函数,该函数首先从 `txt1` 和 `txt2` 中获取两个整数,然后计算它们的最小公倍数,并将结果显示在 `txt3` 中。如果输入的内容不是整数,则会在 `txt3` 中显示一个提示信息。
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)