Tkinter:合并 2 个窗口 [英] Tkinter: Merging 2 windows

查看:50
本文介绍了Tkinter:合并 2 个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩我的第一个 Python Tkinter GUI.在你下面找到我制作的脚本.老实说,我已经在互联网上环顾四周,想知道如何去做.

I've been playing around with my first Python Tkinter GUI. Below you van find te script I've made. Must be honest, I've looked around on the internet to find out how to do it.

当我现在运行我的脚本时,我得到了 2 个单独的窗口.一个窗口,我们称之为窗口 A",我的文本和输入框以及一个空窗口,我们称之为窗口 B".

When I run my script now I get 2 seperate windows. One window, lets call this "window A", with my text and input boxes and one empty window, lets call this "window B".

当我在窗口 A"中单击运行"时,我的 phyton 脚本(在本例中为 tennisMatchProbability.py)被触发,并且该脚本(tennisMatchProbability.py)的结果显示在窗口 B"中.

When I click on "Run" in "window A" my phyton script(tennisMatchProbability.py in this case) is triggered and the results of that script (tennisMatchProbability.py) are displayed in "window B".

这是tennisMatchProbability.py"给出的输出.

This is the output that "tennisMatchProbability.py" gives.

Server Game = 0.735729230769
Receiver Game= 0.264270769231
Tiebreak = 0.337026817252
Server Set = 0.205146215901
Receiver Set= 0.794853784099
Match Server= 0.108987765053
Match Receiver= 0.891012234947

我想要实现的是将两个窗口合并为一个窗口.

What I would like to achieve is that both windows are merged into one window.

我一直在尝试我能想到但无法弄清楚的一切.

I've been trying everything that I could think of but can't figure it out.

from Tkinter import *
import sys
sys.path.append("C:\Users\Magali\Desktop\Tennis\tennisMatchProbability.py")

class App(Frame):
    def run_script(self):
        sys.stdout = self
        try:
            del(sys.modules["tennisMatchProbability"])
        except:
            ## Yeah, it's a real ugly solution...
            pass
        import tennisMatchProbability
        tennisMatchProbability.matchProb()
        sys.stdout = sys.__stdout__


    def build_widgets(self):
        self.text1 = Text(self)
        self.text1.pack(side=TOP)


        master = Tk()
        Label(master, text="First Name").grid(row=0)
        Label(master, text="Last Name").grid(row=1)
        Label(master, text="Game Score").grid(row=2)
        Label(master, text="Set Score").grid(row=3)

        e1 = Entry(master)
        e2 = Entry(master)
        e3 = Entry(master)
        e4 = Entry(master)
        e1.delete(0,END)
        e2.delete(0,END)
        e3.delete(0,END)
        e4.delete(0,END)


        e1.insert(10,"Novak")
        e2.insert(10,"Djokovic")
        e3.insert(10,"30-15")
        e4.insert(10,"3-1")


        e1.grid(row=0, column=1)
        e2.grid(row=1, column=1)
        e3.grid(row=2, column=1)
        e4.grid(row=3, column=1)

        Button(master, text='Run', command=self.run_script).grid(row=4, column=1, sticky=W, pady=4)



    def write(self, txt):
        self.text1.insert(INSERT, txt)

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.build_widgets()


root = Tk()
app = App(master = root)
app.mainloop()

推荐答案

答案很简单:在 build_widgets 方法中,您正在使用

The answer is easy: In the build_widgets method you are constructing a new Tk frame and tcl interpreter with

        master = Tk()

您的应用程序中永远不应该有两个 Tk() 调用.解决方案是删除这一行,并将 master 的每次出现更改为 self.Self 代表您的应用类,该类继承自 tk.Frame 类,因此是您的主框架.

You should never have two Tk() calls in your application. The solution is to delete this line and change every occurance of master to self. Self represent your app class, which inherits from the tk.Frame class and is therefore your main frame.

你的 run_scipt 构造也很奇怪.你为什么不这样做?

Also your construction of run_scipt is rather weird. Why don't you do it like this?

    def run_script(self):
        inputs = self.read_tk_fields()
        result = tennisMatchProbability.matchProb(inputs)

这是完整的代码

from Tkinter import *
import sys
sys.path.append("C:\Users\Magali\Desktop\Tennis\tennisMatchProbability.py")
import tennisMatchProbability

class App(Frame):

    def run_script(self):
        inputs = self.read_tk_field()
        result = tennisMatchProbability.matchProb(inputs)
        self.show_prob_result(result)

    def show_prob_result(self,result):
        self.result_label.config(text=result)

    def build_widgets(self):

        Label(self, text="First Name").grid(row=0)
        Label(self, text="Last Name").grid(row=1)
        Label(self, text="Game Score").grid(row=2)
        Label(self, text="Set Score").grid(row=3)

        e1 = Entry(self)
        e2 = Entry(self)
        e3 = Entry(self)
        e4 = Entry(self)
        self.result_label = Label(self)

        e1.insert(10,"Novak")
        e2.insert(10,"Djokovic")
        e3.insert(10,"30-15")
        e4.insert(10,"3-1")

        e1.grid(row=0, column=1)
        e2.grid(row=1, column=1)
        e3.grid(row=2, column=1)
        e4.grid(row=3, column=1)
        self.result_label.grid(row=4, column=1)

        Button(self, text='Run', command=self.run_script).grid(row=5, column=1, sticky=W, pady=4)

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.build_widgets()


root = Tk()
app = App(master = root)
app.mainloop()

这篇关于Tkinter:合并 2 个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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