Tkinter GUI使用带有函数的单独Python文件实现 [英] Tkinter GUI implemented with separate Python file with functions

查看:99
本文介绍了Tkinter GUI使用带有函数的单独Python文件实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了通过第二个文件实现GUI界面的问题,该文件只包含要读取的文件,绘制的图表和一些基于此要评估的新函数。



我正在尝试使用Tkinter创建GUI应用程序。我的方式如下。我有一个后台脚本(比如 Background.py ),它有两个功能。函数X加载数据文件,进行一些计算并输出图形。我想要触发这个的方法是通过另一个文件( GUI.py )中的GUI脚本打开带有按钮的面板,当我单击按钮时,文件背景中的函数X.应该评估py 并显示一个图。一旦我检查了图,我可以点击另一个按钮关闭图并终止函数X.现在我可以选择单击另一个按钮来触发文件 Background.py 中的函数Y.这些按钮应该允许我输入三个值,这些值应该是文件 Background.py中函数Y的输入。一旦我按下这个按钮,它应该触发函数Y并执行它的操作要求它做。现在最后,我可以按下按钮关闭gui。



我该怎么做?一般粗略的想法会有所帮助。



我尽可能多地举了一个例子:(至少是代码的骨架)

我有一个背景文件说( Background.py )和gui文件(比如 GUI.py



**背景.py **



  import  numpy 
import matplotlib.pyplot as plt
导入 pandas

def progX():
df = pd.read(myfile)

#df.stats#做某事并从文件生成图

plt.boxplot(df [' col'])

plt.show()

def progY(y1,y2,y3):

#从用户输入的GUI界面获取y1,y2,y3

运行代码...并生成输出文件



** GUI.py **

  import 背景 as  bg 
来自 tkinter import *
from tkinter.ttk import *

class GUI():

def 创建小部件(个体经营):
....

def create_panel2(self):
cr eate按钮
panel1 = ...
btn1 =按钮(panel1,text = yyyyy,command = bg.progA)
btn1.pack()

def create_panel2(self ):
创建按钮
panel2 = ...
btn2 =按钮(panel1,text = yyyyy,command = bg.progB)
btn2.pack()

All_Entries = []

window = Tk()
D = GUI(窗口)
window.mainloop()
runprogram1 = bg.progX()
runprogram2 = bg.probY(x,y,z)



我现在的问题是,上面做了什么感?如何从GUI调用后台函数? runprogram1& runprogram2绝对不正确,我该如何实现呢。另外,一旦关闭程序X的输出,我将如何确保在后台调用proram Y?



我想问题是有道理的。我是GUI新手,很难解决这个问题,我需要这样做。任何帮助将非常感谢。



我尝试过:



我试图将骨架放到代码中..但是没有任何工作。

解决方案

是的,上面的方法看起来是正确的,虽然你似乎有不同的程序名称附加到您的按钮。一般而言,GUI应用程序以空白窗口开始。然后,它根据用户的输入显示信息。因此,当您按下按钮(或选择菜单项)时,它将执行以下两项操作之一:

1.按照设计直接生成数据显示,或者

2.显示一个弹出窗口,询问用户一些会影响要显示的数据的输入。

只要用户按下按钮或选择菜单项,就会重复此操作。

I am having problem with having a GUI interface to be implemented via a second file which just contains the file to read, plots made and some new functions to be evaluated based on that.

I am trying to create a GUI application using Tkinter. The way I am doing is as follows. I have a background script (say Background.py) which has two functions. Function X loads a data file, does some calculations and outputs a graph. The way I want to trigger this is via a GUI script in another file (GUI.py) which opens a panel with a button and when I click the button the function X in file Background.py should be evaluated and a plot should be shown. Once I check the plot, I can hit another button to close the plot and terminate the function X. Now I can choose to click another button to trigger the function Y in the file Background.py. These button should allow me to input three values, which should be the input to the function Y in the file Background.py. Once I hit this button, it should trigger the function Y and do what it it asks it to do. Now at the end, after that I can hit the button to close the gui.

How can I do this?. A general rough idea will be helpful.

I have put an example as much as I can:( at least the skeleton of the code)
I have a background file say (Background.py) and gui file ( say GUI.py)

**Background.py**

import numpy
import matplotlib.pyplot as plt
import pandas

def progX():
     df = pd.read (myfile)

     ##df.stats # doing something and generating a plot from the file

    plt.boxplot(df['col'])

    plt.show()

def progY(y1, y2,y3):

    ## get the y1, y2, y3 from the GUI interface which the user has entered

    #run a code...  and generate an output file


**GUI.py**

import Background as bg   
    from tkinter import *
    from tkinter.ttk import *
    
    class GUI ():
        
        def create widgets(self):
            #....
        
        def create_panel2(self):
            #create buttons
            panel1 = ...
            btn1 = Button(panel1, text="yyyyy", command=bg.progA)
            btn1.pack() 
        
        def create_panel2(self):
            #create buttons
            panel2 = ...
            btn2 = Button(panel1, text="yyyyy", command=bg.progB)
            btn2.pack() 
    
    All_Entries = []
    
    window = Tk()
    D=GUI(window)
    window.mainloop()
    runprogram1 = bg.progX()
    runprogram2 = bg.probY(x, y, z)


My question is now, does the above makes sense? How can I call the background functions from the GUI? The statements runprogram1 & runprogram2 are definitely not correct, How can I implement that. Also how will I ensure that I call the proram Y in Background once I have close the output from the program X?

I guess the questions makes sense. I am new to GUI and having hard time working this out, which I need to. any help will be very much appreciated.

What I have tried:

I have tried to put the skeleton to the code.. But nothing working as of yet.

解决方案

Yes the above approach looks correct, although you seem to have different program names attached to your buttons. In general terms a GUI application starts with a blank window. It then displays information based on inputs from the user. So when you press a button (or select a menu item) it will do one of two things:
1. go straight into producing the data display as per the design, or
2. show a popup window that asks the user for some input that will affect the data that is to be displayed.
This will be repeated as long as the user presses buttons or selects menu items.


这篇关于Tkinter GUI使用带有函数的单独Python文件实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆