Git Product home page Git Product logo

Comments (5)

dsl101 avatar dsl101 commented on June 1, 2024

This is what I've tried so far, which just seems to lock up (nothing displayed, python.exe running at 100% CPU):

def callback(self):
    print self.pw.get()

def populateView(self):
    self.pw = tk.Entry(self.master)
    self.pw.pack()
    self.pw.focus_set()

    self.ok = tk.Button(self.master, text="OK", width=10, command=self.callback)
    self.ok.pack()

I called populateView() from within _create_ui(), after loading the .ui file and setting up some Treeview bits and pieces. The idea is that populateView() will ask the user for a password and then decrypt some data into the Treeview object. But clearly I'm Not Doing It Right... I also tried self.toplevel in the call to tk.Entry with the same result. This page suggests this is what toplevel is for, but maybe I'm being too simple in this approach? http://effbot.org/tkinterbook/toplevel.htm

from pygubu.

alejandroautalan avatar alejandroautalan commented on June 1, 2024
Do you have any advice on the best way to add things like 
popups (text input, confirmation dialogues, etc.)? 
I presume it's OK to mix straight tk code in with the pygubu.TkApplication class?

Yes is Ok to mix straight tk code in with the pygubu.TkApplication class.
The main purpose of the gui builder is to try to set up all the graphic properties of the widgets using a nice interface, and everything else that the gui designer can not do needs to be coded.

For simple confirmation dialogs you have the tkMessageBox module with functions like 'askquestion', 'askokcancel'.
For simple input dialogs you have the tkSimpleDialog module with the functions 'askinteger', 'askfloat' 'askstring'.

For example to ask a password you can use:

import Tkinter as tk
import tkSimpleDialog as tksd

...

def populateView(self):
    password = tksd.askstring('Enter password:', '', show='*')
    print password

For more complex dialogs you must extend a dialog class included in those modules.
Please note that those modules don't use the new themed widgets from ttk.

Im working on a Dialog class and widget that can be designed in the gui builder, but is not finished. I pushed some new code to the repo but no documentation yet.

Hope this helps.

from pygubu.

dsl101 avatar dsl101 commented on June 1, 2024

Fab - thanks. askstring is perfect. I'd used tkMessageBox before, but I'd been searching for tkInputBox and not found tkSimpleDialog!

from pygubu.

dsl101 avatar dsl101 commented on June 1, 2024

OK - sorry, one more question (I guess until the dialog class / widget is ready). I now need a progress bar on startup, as there's a lot of data munging to be done and currently no user feedback. Two options I guess:

  • Add a progress bar to the main app (not ideal) - but how to get the UI to show and then trigger the data loading without any user action? I wondered if _init_after() might work, but it still seems to be executed before the UI is shown.
  • Back to my original question, if I want to use a regular bit of tk widgetery, how do I create / show / update it alongside the pygubu stuff, without locking everything up? For example, can I put a ttk.Progressbar()into _create_ui() somehow?

If I do this in _create_ui():

pb = ttk.Progressbar(self.mainWindow, mode='determinate', value=0)
pb.pack()
self.populateView()

Then everything runs (self.populateView() has some debugging code so I can see it's working in the console window), but neither the progress bar nor the main UI are every shown, and the python.exe process goes to 100% CPU.

from pygubu.

alejandroautalan avatar alejandroautalan commented on June 1, 2024

Hello David

* Add a progress bar to the main app (not ideal) - but how to get the UI to show 
and then trigger the data loading without any user action?
I wondered if _init_after() might work, but it still seems to be 
executed before the UI is shown.

In this case I think that the TkApplication class will not help you much, because you need more control on how and when to create the widgets. The TkApplication class by default hides the root window, call _init_before, _create_ui, _init_after and then shows the root window again. You will need to override the constructor for more control.

However, you can use the new tk.Toplevel and Dialog widget classes. You can use the builder to define them.
The tk.Toplevel is the Toplevel widget directly from tkinter, and the Dialog is a custom pygubu class to make easier the creation of dialog windows. With these you have more control to create the widgets.

About your problem, creating a progressbar when loading. One of the simplest way is to use the widget method 'update' inside the loading function to show the progress.
Below I'll show an example of how I would solve your problem. Remember that: this might not be the best way for you, does not use the pygubu.TkApplication class, and you will need the latest pygubu version.

<?xml version="1.0" ?>
<interface>
  <object class="tk.Toplevel" id="mainwindow">
    <property name="geometry">440x380</property>
    <property name="height">200</property>
    <property name="padx">2</property>
    <property name="pady">2</property>
    <property name="resizable">both</property>
    <property name="title" translatable="yes">My Application</property>
    <property name="width">200</property>
    <child>
      <object class="ttk.Frame" id="Frame1">
        <property name="height">200</property>
        <property name="width">200</property>
        <layout>
          <property name="column">0</property>
          <property name="propagate">True</property>
          <property name="row">0</property>
          <property name="sticky">nesw</property>
          <rows>
            <row id="1">
              <property name="weight">1</property>
            </row>
          </rows>
          <columns>
            <column id="0">
              <property name="weight">1</property>
            </column>
          </columns>
        </layout>
        <child>
          <object class="ttk.Label" id="Label_1">
            <property name="text" translatable="yes">Main Window</property>
            <layout>
              <property name="column">0</property>
              <property name="propagate">True</property>
              <property name="row">0</property>
            </layout>
          </object>
        </child>
        <child>
          <object class="pygubu.builder.widgets.scrollbarhelper" id="scrollbarhelper_1">
            <property name="scrolltype">both</property>
            <layout>
              <property name="column">0</property>
              <property name="propagate">True</property>
              <property name="row">1</property>
              <property name="sticky">nesw</property>
            </layout>
            <child>
              <object class="ttk.Treeview" id="treeview1">
                <property name="selectmode">extended</property>
                <layout>
                  <property name="column">0</property>
                  <property name="propagate">True</property>
                  <property name="row">1</property>
                  <property name="sticky">nesw</property>
                </layout>
                <child>
                  <object class="ttk.Treeview.Column" id="datacol">
                    <property name="column_anchor">w</property>
                    <property name="heading_anchor">w</property>
                    <property name="minwidth">20</property>
                    <property name="stretch">True</property>
                    <property name="text" translatable="yes">Data</property>
                    <property name="tree_column">True</property>
                    <property name="visible">True</property>
                    <property name="width">200</property>
                  </object>
                </child>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
  <object class="pygubu.builder.widgets.dialog" id="progressdialog">
    <property name="height">100</property>
    <property name="modal">True</property>
    <property name="padx">4</property>
    <property name="pady">4</property>
    <property name="resizable">none</property>
    <property name="title" translatable="yes">Wait</property>
    <property name="width">200</property>
    <child>
      <object class="ttk.Label" id="Label_2">
        <property name="justify">center</property>
        <property name="text" translatable="yes">Loading application data
Please wait ...</property>
        <layout>
          <property name="column">0</property>
          <property name="propagate">True</property>
          <property name="row">0</property>
        </layout>
      </object>
    </child>
    <child>
      <object class="ttk.Progressbar" id="progressbar1">
        <property name="maximum">10</property>
        <property name="mode">indeterminate</property>
        <property name="orient">horizontal</property>
        <layout>
          <property name="column">0</property>
          <property name="propagate">True</property>
          <property name="row">1</property>
          <property name="sticky">ew</property>
        </layout>
      </object>
    </child>
  </object>
</interface>
# dialogtest.py
import time
import Tkinter as tk
import pygubu


class MyApplication:
    def __init__(self):
        self.builder = builder = pygubu.Builder()
        builder.add_from_file('dialogtest.ui')

        self.mainwindow = mainwindow = builder.get_object('mainwindow')
        self.progressdialog = dialog = builder.get_object('progressdialog', self.mainwindow)

        self.show_load_dialog()

    def show_load_dialog(self):
        self.mainwindow.update()
        self.center_dialog()
        #show dialog
        self.progressdialog.run()
        self.load_data()


    def center_dialog(self):
        #calculate middle point
        dialog = self.progressdialog
        x = self.mainwindow.winfo_rootx()
        y = self.mainwindow.winfo_rooty()
        x = x + (self.mainwindow.winfo_width() /2) - (dialog.toplevel.winfo_reqwidth() / 2)
        y = y + (self.mainwindow.winfo_height() /2) - (dialog.toplevel.winfo_reqheight() / 2)
        geometry = '+{0}+{1}'.format(x, y)
        dialog.toplevel.geometry(geometry)


    def load_data(self):
        print 'loading data'
        tv = self.builder.get_object('treeview1')
        pb = self.builder.get_object('progressbar1')
        pb.start()
        for i in range(1, 100):
            #
            #simulate work, sleep 1/10 seconds
            #
            time.sleep(0.1)
            tv.insert('', tk.END, text='item {0}'.format(i))
            #
            #update windows
            #
            self.mainwindow.update()
        self.progressdialog.close()

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


if __name__ == '__main__':
    app = MyApplication()
    app.run()
* Back to my original question, if I want to use a regular bit of tk widgetery, how do I 
create / show / update it alongside the pygubu stuff, without locking everything up?
For example, can I put a ttk.Progressbar()into _create_ui() somehow?

You can create any widget by code, but you need to remember NOT mix layout managers.
Pygubu uses the grid layout manager so, for example, if you need to add a widget
inside a frame wich has additional widgets defined in the pygubu builder
you must use the grid method and not pac or place.

Regards

from pygubu.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.