Приложение Python GUI с более чем одним окном и для мониторинга различных операций - PullRequest
0 голосов
/ 08 июля 2019

У меня есть приложение с графическим интерфейсом Python tkinter. Он показывает некоторые данные датчика каждые секунды. Поэтому я использую многопоточность и очередь для обновления данных на экране графического интерфейса пользователя (в окне 1 или в главном окне). Теперь я хочу добавить кнопку. И нажатие этой кнопки должно открыть новое окно (окно-2), чтобы выполнить какую-то другую операцию сенсорного управления. Это окно-2 должно остановить работу окна-1. можно ли так сделать? и возможно ли добавить функциональность, например, закрытие окна -2 отменит и активирует операцию окна-1?

#Created by Jacob Hallén, AB Strakt, Sweden. 2001-10-17
#I am using this example

import tkinter
import time
import threading
import random
import queue


class GuiPart:
    def __init__(self, master, queue, endCommand):
        self.queue = queue
        # Set up the GUI
        console = tkinter.Button(master, text='Done', command=endCommand)
        console.place(x=15, y=400)

        Button_sensor  = tkinter.Button(master, text = "Sensor Control", command=secondWindow)
        Button_sensor.place(x=600, y=300)
#the above button should call secondWindow method and how to stop the #activities in the window 1? and go to window-2? and come back to window-1 #after closing window-2



    def processIncoming(self):

        while self.queue.qsize():
            try:
                msg = self.queue.get(0)
                # Check contents of message and do what it says
                # As a test, we simply print it
                print (msg)
            except queue.Empty:
                pass
     def secondWindow(self):
       # function definition here
       # any suggestions?


class ThreadedClient:
    def __init__(self, master):
        self.master = master

        # Create the queue
        self.queue = queue.Queue()

        # Set up the GUI part
        self.gui = GuiPart(master, self.queue, self.endApplication)

        self.running = 1
        self.thread1 = threading.Thread(target=self.workerThread1)
        self.thread1.start()
        self.periodicCall()

    def periodicCall(self):
        """
        Check every 1000 ms if there is something new in the queue.
        """
        self.gui.processIncoming()
        if not self.running:
            import sys
            sys.exit(1)
        self.master.after(1000, self.periodicCall)

    def workerThread1(self):
        while self.running:
            time.sleep(rand.random() * 1)
            msg = rand.random()
            self.queue.put(msg)

    def endApplication(self):
        self.running = 0

rand = random.Random()
root = tkinter.Tk()

client = ThreadedClient(root)
root.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...