[英]Use GUI to run multiple instances of a script with different variables
使用给定的 GUI,如果选择启用/禁用测试,我想使用给定的测试名称、IP 地址、发送电子邮件参数启动脚本。
processes={}
if (start_stop_checkbut1.get()):
processes['192.168.x.xx'] = {
'name': test_name_var1.get(),
'ip': ip_var1.get(),
'enable_disable': start_stop_checkbut1.get(),
'email': eb1.get(),
'email_address': email_address1.get()
}
call_main_script('192.168.x.xx', processes)
我对 GUI 编程相当陌生,所以我在这里的目标是选择启用/禁用的哪一行,可以使用参数启动脚本,我希望它们同时运行(也可以在命令提示符的多个实例中运行)。
我不知道如何实现这一点,我需要做多进程或子进程吗? 如果是这样,我能得到一些关于这方面的指导吗?
您的问题与 Tkinter 和 GUI 编程没有真正的联系。 您需要多线程或多处理。 有效的解决方案取决于您想要做什么。 您可以看到以下有关选择多线程/多处理的页面: This和This
老实说,根据您的问题,您的问题对我来说不是很清楚,但我已尽力回答。
您可以在下面看到一个测试代码,没有任何并行性,因此它不是您的解决方案。
代码:
"""
Example script for data processing
"""
def call_main_script(ip_adr_str, processes_dict):
"""
Print the data structure for testing.
Args:
ip_adr_str: The IP address as string to define the dict key.
processes_dict: The dict data structure
Returns: None
"""
# Get the key-values based on the given IP address
for key, value in processes_dict[ip_adr_str].items():
print("{} -> {}".format(key, value))
print("\n\n")
class DummyCheckBox:
"""
A Dummy class for simulate the CheckBox widget of TK.
"""
def __init__(self, number_of_box):
"""
The constructor of DummyCheckBox
Args:
number_of_box: Input parameter of "widget"
"""
self.number_of_box = number_of_box
def get(self):
"""
Provides some dummy value (Class name and the input value...).
Returns: Dummy str.
"""
return "{}_{}".format(self.__class__.__name__, self.number_of_box)
class DummyName:
"""
A Dummy class for simulate the Input widget of TK.
"""
def __init__(self, name_input):
"""
The constructor of DummyName
Args:
name_input: Input parameter of "widget"
"""
self.name = name_input
def get(self):
"""
Provides some dummy value (Class name and the input value...).
Returns: Dummy str.
"""
return "{}_{}".format(self.__class__.__name__, self.name)
# Creating the instances from the Check Box class.
dummy_check_box_1 = DummyCheckBox(1)
dummy_check_box_2 = DummyCheckBox(2)
dummy_check_box_3 = DummyCheckBox(3)
# Creating the instances from the Name widget class.
dummy_name_1 = DummyName("FOO")
dummy_name_2 = DummyName("BAR")
dummy_name_3 = DummyName("BAZ")
processes = {}
# Put the instances to lists.
check_boxes = [dummy_check_box_1, dummy_check_box_2, dummy_check_box_3]
names = [dummy_name_1, dummy_name_2, dummy_name_3]
# You can process the lists in ony for loop with the "zip" function.
for check_box, name in zip(check_boxes, names):
if check_box.get():
processes["192.168.x.xx"] = {
"name": name.get(),
"ip": "dummy_ip",
"enable_disable": check_box.get(),
"email": "dummy_mail",
"email_address": "dummy_adress",
}
call_main_script("192.168.x.xx", processes)
输出:
>>> python3 test.py
name -> DummyName_FOO
ip -> dummy_ip
enable_disable -> DummyCheckBox_1
email -> dummy_mail
email_address -> dummy_adress
name -> DummyName_BAR
ip -> dummy_ip
enable_disable -> DummyCheckBox_2
email -> dummy_mail
email_address -> dummy_adress
name -> DummyName_BAZ
ip -> dummy_ip
enable_disable -> DummyCheckBox_3
email -> dummy_mail
email_address -> dummy_adress
以下示例包含一个多线程解决方案。 我已经评论了代码中所有更改的地方。
代码:
"""
Example script for data processing
"""
import threading
import random
import time
def call_main_script(processes_dict):
"""
Print the data structure for testing.
Args:
processes_dict: The dict data structure
Returns: None
"""
# Get a random integer between 1-5 and sleep it in secs.
random_sleep = random.randint(1, 5)
time.sleep(random_sleep)
print(
"[{}] ({} secs slept) name -> {}".format(
threading.current_thread().name, random_sleep, processes_dict["name"]
)
)
class DummyCheckBox:
"""
A Dummy class for simulate the CheckBox widget of TK.
"""
def __init__(self, number_of_box):
"""
The constructor of DummyCheckBox
Args:
number_of_box: Input parameter of "widget"
"""
self.number_of_box = number_of_box
def get(self):
"""
Provides some dummy value (Class name and the input value...).
Returns: Dummy str.
"""
return "{}_{}".format(self.__class__.__name__, self.number_of_box)
class DummyName:
"""
A Dummy class for simulate the Input widget of TK.
"""
def __init__(self, name_input):
"""
The constructor of DummyName
Args:
name_input: Input parameter of "widget"
"""
self.name = name_input
def get(self):
"""
Provides some dummy value (Class name and the input value...).
Returns: Dummy str.
"""
return "{}_{}".format(self.__class__.__name__, self.name)
# Creating the instances from the Check Box class.
dummy_check_box_1 = DummyCheckBox(1)
dummy_check_box_2 = DummyCheckBox(2)
dummy_check_box_3 = DummyCheckBox(3)
# Creating the instances from the Name widget class.
dummy_name_1 = DummyName("FOO")
dummy_name_2 = DummyName("BAR")
dummy_name_3 = DummyName("BAZ")
# This list will contain the dict structures.
processes = []
# Put the instances to lists.
check_boxes = [dummy_check_box_1, dummy_check_box_2, dummy_check_box_3]
names = [dummy_name_1, dummy_name_2, dummy_name_3]
# You can process the lists in ony for loop with the "zip" function.
for check_box, name in zip(check_boxes, names):
if check_box.get():
# Creating the dict data structure.
data_structure = {
"name": name.get(),
"ip": "dummy_ip",
"enable_disable": check_box.get(),
"email": "dummy_mail",
"email_address": "dummy_adress",
}
# Append the actual dict data structure to the process list.
processes.append(data_structure)
# Creating the list for the started threads. We will wait the threads based on this list.
threads = []
# Creating threads for the dict data structures.
for single_dict in processes:
# Creating the thread which calls the "call_main_script" function with "single_dict" argument.
t = threading.Thread(target=call_main_script, args=(single_dict,))
# Start the actual thread.
t.start()
# Append the Thread object to the list.
threads.append(t)
# Join (Stop) the Threads one-by-one.
for t in threads:
# Wait until thread is completely executed
t.join()
print("Done!")
输出:
>>> python3 test.py
[Thread-3] (1 secs slept) name -> DummyName_BAZ
[Thread-1] (2 secs slept) name -> DummyName_FOO
[Thread-2] (5 secs slept) name -> DummyName_BAR
Done!
正如您在上面的输出部分中看到的,3 个不同的 dict 数据结构在 3 个线程上启动。 我添加了一个随机(1-5 秒)睡眠以获得更好的表现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.