python-3.x 如何从tkinter脚本运行代码脚本?

v8wbuo2f  于 2023-01-03  发布在  Python
关注(0)|答案(1)|浏览(127)

I try to use tkinter to run the test.py script. In tkinter GUI, I had 4 button , but currently only 2 button I want to use for test.py :
Connecting button which open the test.py file Home cablibrate which run the print "Hi" in test.py
我想要的:
So when user press connecting button , the test.py will open and print "welcome" on the terminal ( test.py running parallel with the tkinter GUI ). After that , if the button home cablibrate is pressing , the tkinter GUI will pass the value n equal 2 to the test.py and run the condition n>2 in test.py to print "hi"
发布日期:
1/ I try to use subprocess in tkinter to open and print "welcome" on test.py, but nothing print out , it only add different value into my test.py.I think i use the wrong function. However , I not sure how to fix this ?
2/ Since there is a error when open test.py and print . I havent write the pass parameter condition for home cablibrate function. I also not sure how to pass communicate between two script. Could you guy give me some advice on this ?
时间:

import tkinter as tk
from tkinter import ttk 
from subprocess import Popen, PIPE 
 

class graphic(tk.Tk,Popen):
    def __init__(self):
        super().__init__()
        self.reset_clock = 0
        self.check_value = 0 
        self.done = 0
        self.minute = 1 
        self.second = 0   
        self.title('Automation Cleaning Process')
        self.geometry('1100x800+100+100')
        self.resizable(False, False)
        self.menubar = tk.Menu(self)
        filemenu = tk.Menu(self.menubar, tearoff=0)
        filemenu.add_command(label="New")
        filemenu.add_command(label="Open")
        filemenu.add_command(label="Save")
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.quit)
        self.menubar.add_cascade(label="File", menu=filemenu)
        
        helpmenu = tk.Menu(self.menubar, tearoff=0)
        helpmenu.add_command(label="Help Index")
        helpmenu.add_command(label="About...")
        self.menubar.add_cascade(label="Help",menu=helpmenu)
        self.integer_variable_x_value = tk.IntVar()
        self.integer_variable_y_value = tk.IntVar()
        self.integer_variable_z_value = tk.IntVar() 
        self.integer_variable_r_value = tk.IntVar() 
        self.integer_variable_x_value.set(0)
        self.integer_variable_y_value.set(0)
        self.integer_variable_z_value.set(0)
        self.integer_variable_r_value.set(0) 
        
        self.tabControl = ttk.Notebook(self) 
        self.tab_1 = ttk.Frame(self.tabControl)
        self.tab_2 = ttk.Frame(self.tabControl)
        self.tabControl.add(self.tab_2,text = 'HOME')
        self.tabControl.add(self.tab_1,text = 'Option')
        self.tabControl.pack(expand = 1, fill ="both")
        
        self.set_button()
        self.set_text_gui()
        self.set_timer_counter_process(self.minute,self.second)
        self.update_output(200,300,400,500)
        self.config(menu=self.menubar)

    def set_button(self):
        self.button_1 = ttk.Button (self.tab_1,text = "Home Cabliration", command = self.button_1_clicked,  width = 50) 
        self.button_1.place(x=380,y=200)
        self.button_2 = ttk.Button(self.tab_1, text='Automation Process',command=self.button_2_clicked_clock_counting_down, width = 50)
        self.button_2.place(x=380,y=300)
        self.button_3 = ttk.Button(self.tab_1, text='Stop',command=self.button_3_clicked, width = 50)
        self.button_3.place(x=380,y=400)
        self.button_connected = ttk.Button (self.tab_2,text = "Connecting Dobot" , command=self.button_connected_clicked, width=50)
        self.button_connected.place(x=400,y=300)
        
    

    def button_connected_clicked(self): 
        self.connecting = tk.Label(self.tab_2,text = "Connecting ...", font=("Inter", 25))
        self.connecting.place(x = 450  , y = 350)
        self.dobot_COM_port()
            

    def dobot_COM_port(self): 
        self.source ='C:/Users/ching/OneDrive/Desktop/DobotGUI/test.py'
        self.file_run  = open(self.source,'w+')
        self.process_run_parallel = Popen(["ls","-all"], universal_newlines=True, stdout=self.file_run, stderr=PIPE)
        
        

    def button_1_clicked(self): 
     self.button_1_value = 1
     print (self.button_1_value)
     print ("button 1 click ")
   
    def button_2_clicked_clock_counting_down(self):
     self.done = 1 
     self.button_2_value = 2
     print (self.button_2_value)
     print ("button 2 click ")

    def button_3_clicked(self): 
     self.button_3_value = 3
     print (self.button_3_value)
     print ("button 3 click ")

 
    def set_text_gui(self): 
        #declare label 
        self.x_axis = tk.Label(self.tab_2,text = "x value:", font=("Inter", 25))
        self.y_axis = tk.Label(self.tab_2, text = "y value:", font=("Inter",25))
        self.z_axis = tk.Label(self.tab_2, text = "z value:", font=("Inter",25))
        self.r_axis = tk.Label(self.tab_2, text = "r value:", font=("Inter",25))
    
    
        self.x_axis_value = tk.Label(self.tab_2, text = "x value:", font=("Inter",25))
        self.y_axis_value= tk.Label(self.tab_2, text = "y value:", font=("Inter", 25))
        self.z_axis_value = tk.Label(self.tab_2, text = "z value:", font=("Inter",25))
        self.r_axis_value= tk.Label(self.tab_2, text = "r value:", font=("Inter", 25))

        self.time_title = tk.Label(self.tab_2, text = "Time Process:", font=("Inter",30))
        self.time_colon= tk.Label(self.tab_2, text = ":", font=("Inter",  30))
        self.min_label= tk.Label(self.tab_2, text = "min", font=("Inter", 30))
        self.second_label=tk.Label(self.tab_2, text = "sec", font=("Inter",30))
        self.label_minute = tk.Label(self.tab_2,font=("Inter",30))
        self.label_second = tk.Label (self.tab_2,font=("Inter",30))
        self.label_minute_zero_add = tk.Label(self.tab_2,font=("Inter",30))
        self.label_second_zero_add = tk.Label(self.tab_2,font=("Inter",30))
        # position label 
        
        self.x_axis.place(x = 85  , y = 150)
        self.y_axis.place(x = 330 , y = 150)
        self.z_axis.place(x = 575 , y = 150)
        self.r_axis.place(x = 820 , y = 150)

        self.x_axis_value.place(x = 200 , y = 150)
        self.y_axis_value.place(x = 445 , y = 150)
        self.z_axis_value.place(x = 695 , y = 150)
        self.r_axis_value.place(x = 935 , y = 150)

        self.time_title.place(x=85,y=590) 
        self.time_colon.place(x =230,y=650)
        self.min_label.place(x=150,y=650)
        self.second_label.place(x=335,y=650)
        self.label_minute.place(x = 120 , y = 650)
        self.label_second.place(x=  278 , y = 650)
        self.label_minute_zero_add.place(x=  90 ,y=650)
        self.label_second_zero_add.place(x = 250 ,y= 650)

    def update_output(self , input_dobot_x, input_dobot_y, input_dobot_z , input_dobot_r):
        # update x-axis 
        self.integer_variable_x_value.set(input_dobot_x)
        self.x_axis_value.config(text=str(self.integer_variable_x_value.get()))
        # update y axis 
        self.integer_variable_y_value.set(input_dobot_y)
        self.y_axis_value.config(text=str(self.integer_variable_y_value.get()))
        # update z axis 
        self.integer_variable_z_value.set(input_dobot_z)
        self.z_axis_value.config(text=str(self.integer_variable_z_value.get()))
        
        # update r axis
        self.integer_variable_r_value.set(input_dobot_r)
        self.r_axis_value.config(text=str(self.integer_variable_r_value.get()))
        
        #update all the variable every 100secs to GUI 
        self.after(100,self.update)

    def set_timer_counter_process(self,minute,second):
        print(self.done)  
        self.label_minute['text'] = minute
        self.label_second['text'] = second
        check_minute_length = len(str(minute))
        check_second_length = len(str(second))
        if check_minute_length == 1: 
            self.label_minute_zero_add['text'] = 0 
        else: 
            self.label_minute_zero_add['text'] =''

        if check_second_length == 1:
            self.label_second_zero_add['text'] = 0  
        else:
            self.label_second_zero_add['text'] ='' 

        if self.done == 1:      
            if second  > 0 :
                second = second - 1       
            elif second <= 0 :
                if minute > 0:
                    minute  = minute - 1  
                    second  = 60 
                else:
                    self.done = 0
        else:
            minute = 1
            second = 0           
        self.after(1000, self.set_timer_counter_process, minute , second) 


        

User_graphic = graphic()
User_graphic.mainloop()

test.py:

n = 0 ; 
print("welcome")

if (n > 1): 
    print ("hi")
mrfwxfqh

mrfwxfqh1#

这里你可以做两件事。首先让我解释一下,你不需要子进程来解决这个问题,python内置的“stuff”就可以完成这个任务。如果你想语法正确并且异步,你可以在www.example.com中test.py写:

async def main():
    n=0
    print(“welcome”)

    if n > 1:
        print(“hi”)

然后在你的主tkinter文件中,你可以把这个放在你脚本的顶部:

import main from test

然后当你想调用/运行测试时,你可以这样做:

main()

也可以使用import lib或直接在函数中导入,但不建议这样做

相关问题