简体   繁体   中英

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.

I am VERY new to python, have only just started with it a few days ago. 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.

Because I am new to it, I thought I would try Pygubu, especially since I am using a windows machine . I had a look at their examples and found out how to load the MainWindow which I have designed.

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").

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.

I saw an example that talks about opening child windows at How do I create child windows with Python tkinter? but I am not really sure how that is translated to open a frame from within a UI file?

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?

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: 我编造了一个我从你的规格中理解的最小例子。

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.

在此处输入图片说明

Hopefully this might help someone in the future!

Cheers, Tim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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