简体   繁体   English

Pygubu 从 UI 文件加载新窗口

[英]Pygubu Load new Window from UI File

I have been looking through Pygubu examples trying to best understand how to achieve my goal with no success.我一直在查看Pygubu 示例,试图最好地了解如何实现我的目标,但没有成功。

I am VERY new to python, have only just started with it a few days ago.我对 python 非常陌生,几天前才开始使用它。 I have a number of functions that work well and accept a range of variables to do tasks but now I want to try and create a GUI to handle the inputs before firing up the functions etc.我有许多运行良好的函数并接受一系列变量来执行任务,但现在我想尝试创建一个 GUI 来在启动函数等之前处理输入。

Because I am new to it, I thought I would try Pygubu, especially since I am using a windows machine .因为我是新手,所以我想我会尝试 Pygubu,特别是因为我使用的是Windows 机器 I had a look at their examples and found out how to load the MainWindow which I have designed.我查看了他们的示例,并了解了如何加载我设计的MainWindow

My main window is basically a menu system loaded from Panorama.ui, it has a few buttons which are designed to be pressed to open up other windows ("SphericalWindow", "Gigapixel_Window and "Settings").我的主窗口基本上是一个从 Panorama.ui 加载的菜单系统,它有几个按钮,旨在按下这些按钮来打开其他窗口(“SphericalWindow”、“Gigapixel_Window”和“设置”)。

I have played with the callback function, and that works fine in pulling up a messagebox , however where I get stuck is how to call one of the other windows to be opened.我玩过回调函数,在拉起一个messagebox效果很好,但是我遇到的问题是如何调用要打开的其他窗口之一。

I saw an example that talks about opening child windows at How do I create child windows with Python tkinter?我在如何使用 Python tkinter 创建子窗口? but I am not really sure how that is translated to open a frame from within a UI file?但我不确定如何将其转换为从 UI 文件中打开框架?

How would that example, where a top-level window is created eg below be converted so that tk.Toplevel is being built from a UI file?将如何转换创建顶级窗口的示例,例如下面的tk.Toplevel是从 UI 文件构建的?

t = tk.Toplevel(self)

Thanks for any help.感谢您的帮助。

import sys
import os

try:
    import tkinter as tk
    from tkinter import messagebox
except:
    import Tkinter as tk
    import tkMessageBox as messagebox

sys.path.append(os.path.join(os.path.dirname(__file__), '../'))

import pygubu


class Myapp:
    def __init__(self, master):
        self.builder = builder = pygubu.Builder()
        fpath = os.path.join(os.path.dirname(__file__),"Panorama.ui")
        builder.add_from_file(fpath)

        mainwindow = builder.get_object('MainWindow', master)

        builder.connect_callbacks(self)


    def on_Gigapixel_Click(self):
        #Callback to open window here.

if __name__ == '__main__':
    root = tk.Tk()
    app = Myapp(root)
    root.mainloop()

Although this thread is rather old, I'll still answer it as I stumbled upon it searching for a similar solution which I have figured out by now.虽然这个线程很旧,但我仍然会回答它,因为我偶然发现它正在寻找我现在已经找到的类似解决方案。

My Pygubu file looks as follows:我的 Pygubu 文件如下所示: 我编造了一个我从你的规格中理解的最小例子。

The corresponding code looks like this:对应的代码如下所示:

try:
    import tkinter as tk  # for python 3
    from tkinter import messagebox
except:
    import Tkinter as tk  # for python 2
    import tkMessageBox as messagebox

import threading
import pygubu

class Application:

    def __init__(self, master):

        self.builder = builder = pygubu.Builder()

        builder.add_from_file('Panorama.ui')

        self.mainwindow = builder.get_object('MainWindow', master)

        builder.connect_callbacks(self)

    def openSphericalWindow(self):

        builder2 = pygubu.Builder()
        builder2.add_from_file('Panorama.ui')
        top2 = tk.Toplevel(self.mainwindow)
        frame2 = builder2.get_object('SphericalWindow', top2)
        builder2.connect_callbacks(self)


    def quit(self, event=None):
        self.mainwindow.quit()

    def run(self):
        self.mainwindow.mainloop()

def startUI():

    root = tk.Tk()
    app = Application(root)
    root.mainloop()

    return

if __name__ == '__main__':

    t = threading.Thread(target=startUI)
    t.start()

This then opens a settings window with three buttons of which the SphericalWindow-Button opens a new window.这将打开一个带有三个按钮的设置窗口,其中 SphericalWindow-Button 会打开一个新窗口。

在此处输入图片说明

Hopefully this might help someone in the future!希望这可以在将来对某人有所帮助!

Cheers, Tim干杯,蒂姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM