使用 tkinter 为 Python 程序创建 UI。 Tkinter 中的按钮可以多次按下。如何创建 "One Process" 弹出窗口?
Creating a UI for Python Program using tkinter. The button in Tkinter can be pressed multiple times. How can I create a "One Process" Pop Up?
这是简单的 UI 我对此的尝试...
import tkinter as ttk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter import messagebox
from tkinter.ttk import *
app = ttk.Tk()
app.geometry("400x400")
app.configure(bg='gray')
photo = ttk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png")
myFont = font.Font(family='Helvetica', size=20, weight='normal')
ttk.Label(app, text='Ex', bg='gray', font=(
'Verdana', 15)).pack(side=ttk.TOP, pady=10)
app.iconbitmap(r'C:\Users\ex\ex_icon.ico')
def ex_activation():
global pro
print("Running program!")
pro = subprocess.Popen("python programex.py", shell=True)
def ex_stop():
global pro
print("Stopping Program... Please Wait!")
os.kill(pro.pid, 0)
ex_activation_button = ttk.Button(app, bg='black', image=photo, width=120, height=120, command=ex_activation)
ex_activation_button += 1
if ex_activation_button < 1:
messagebox.showinfo("showinfo", "In Order to Run Again, Press ""Stop Program""")
ex_stop_button = ttk.Button(app, bg='Gray', text='Stop Program', width=12, command=ex_stop, height=3)
ex_stop_button['font'] = myFont
app.title("Ex")
ex_activation_button.pack(side=ttk.TOP)
ex_stop_button.pack(side=ttk.LEFT)
# app.mainloop()
while True:
try:
app.update()
app.update_idletasks()
except KeyboardInterrupt:
pass
此处的目标是在多次单击按钮时添加“仅一个进程”警告。真正的问题是在单击停止按钮后,我将如何重置此计数器以计算单击开始按钮的次数。这是一个循环程序,这就是我有一个停止按钮的原因。非常感谢任何帮助!
您可以通过更改按钮的状态来禁用该按钮。禁用后按钮无法点击
# disable a button:
ex_activation_button['state'] = 'disabled'
# re-enable the button:
ex_activation_button['state'] = 'normal'
# here is the change you need to add:
def ex_activation():
global pro
print("Running program!")
ex_activation_button['state'] = 'disabled'
pro = subprocess.Popen("python programex.py", shell=True)
def ex_stop():
global pro
print("Stopping Program... Please Wait!")
os.kill(pro.pid, 0)
ex_activation_button['state'] = 'normal'
这是简单的 UI 我对此的尝试...
import tkinter as ttk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter import messagebox
from tkinter.ttk import *
app = ttk.Tk()
app.geometry("400x400")
app.configure(bg='gray')
photo = ttk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png")
myFont = font.Font(family='Helvetica', size=20, weight='normal')
ttk.Label(app, text='Ex', bg='gray', font=(
'Verdana', 15)).pack(side=ttk.TOP, pady=10)
app.iconbitmap(r'C:\Users\ex\ex_icon.ico')
def ex_activation():
global pro
print("Running program!")
pro = subprocess.Popen("python programex.py", shell=True)
def ex_stop():
global pro
print("Stopping Program... Please Wait!")
os.kill(pro.pid, 0)
ex_activation_button = ttk.Button(app, bg='black', image=photo, width=120, height=120, command=ex_activation)
ex_activation_button += 1
if ex_activation_button < 1:
messagebox.showinfo("showinfo", "In Order to Run Again, Press ""Stop Program""")
ex_stop_button = ttk.Button(app, bg='Gray', text='Stop Program', width=12, command=ex_stop, height=3)
ex_stop_button['font'] = myFont
app.title("Ex")
ex_activation_button.pack(side=ttk.TOP)
ex_stop_button.pack(side=ttk.LEFT)
# app.mainloop()
while True:
try:
app.update()
app.update_idletasks()
except KeyboardInterrupt:
pass
此处的目标是在多次单击按钮时添加“仅一个进程”警告。真正的问题是在单击停止按钮后,我将如何重置此计数器以计算单击开始按钮的次数。这是一个循环程序,这就是我有一个停止按钮的原因。非常感谢任何帮助!
您可以通过更改按钮的状态来禁用该按钮。禁用后按钮无法点击
# disable a button:
ex_activation_button['state'] = 'disabled'
# re-enable the button:
ex_activation_button['state'] = 'normal'
# here is the change you need to add:
def ex_activation():
global pro
print("Running program!")
ex_activation_button['state'] = 'disabled'
pro = subprocess.Popen("python programex.py", shell=True)
def ex_stop():
global pro
print("Stopping Program... Please Wait!")
os.kill(pro.pid, 0)
ex_activation_button['state'] = 'normal'