Questions about creating a GUI

Post here if you need help with creating a Graphical User Interface in Python.

Questions about creating a GUI

Postby LavaCreeperKing » Tue Aug 23, 2016 8:04 pm

I have started to learn how to make GUIs and I have a few questions.
1: What does sticky and columnspan do?
2: With pygame you can make the pygame widow open in one location on your screen. Is it possible to also do this with a GUI?
3: How do I make a GUI close its self when you push a button(not the x button) and keep the program running? This way you can have new windows come up or other stuff can happen.
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: Questions about creating a GUI

Postby Larz60+ » Wed Aug 24, 2016 2:45 am

Hello,

1: What does sticky and columnspan do?

These are attributes of the grid command.

Sticky basically is alignment, e - east, w - west, n - north and s - south.
used alone, it is the alignment within the bounds of a grid row and column w being left side, e right, etc. they can also be combined. ew will fill the space bwtween left and right, nsew the entire widget space.

columnspan is the number of columns that the widget will span. If column = 2 and the columnspan = 3, then the widget 'is allowed to' use the columns 2, 3 and 4. (if sticky = ew, then the entire width will be used)
Unfortunately you may see nothing, until the widget is populated. also if the widget in column 1 is not active, it will look as though yours starts in column 1. This can be difficult to grasp. I think an understanding of
graphics viewport would help see https://en.wikipedia.org/wiki/Viewport

2: With pygame you can make the pygame widow open in one location on your screen. Is it possible to also do this with a GUI?


Not a pygame user, someone else will have to answer this one.

3: How do I make a GUI close its self when you push a button(not the x button) and keep the program running? This way you can have new windows come up or other stuff can happen.


destroy() - totally removes the window and all children (widgets within the destroyed window)

withdraw() - hides it, but a new grid or pack command will bring it back. see http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/toplevel.html
and http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid.html

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Questions about creating a GUI

Postby LavaCreeperKing » Wed Aug 24, 2016 6:52 pm

Thank you for your help. By the way when I asked: With pygame you can make the pygame widow open in one location on your screen. Is it possible to also do this with a GUI?
What I meant was can I make a GUI window open in one spot. I was not asking about pygame. Anyways thanks for your help and if I need more help I will post about it later.
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: Questions about creating a GUI

Postby LavaCreeperKing » Thu Aug 25, 2016 7:50 pm

Alright new question. So I made this window. I like the way it is but I want to move the button to the bottom right corner. I have it at the right of the window but I can't get it to go to the very bottom. Also when I move the button down I see how the text enter box moves up. How do I get the button to the bottom right corner and keep the text enter box where it is?

Code: Select all
class GUI(Frame):
    def __init__(self, master):
        super(GUI, self).__init__(master)
        self.grid()
        self.objects()
    def gettxt(self):
        self.text = self.get.get()
        print(str(self.text))
        self.num = int(self.text)

    def objects(self):
        self.lbl = Label(self, text = 'words')
        self.lbl.grid(row=0,column=0,sticky=W)
        self.b = Button(self, text = 'click', command = self.gettxt)
        self.b.grid(row=1,column=2,columnspan=2,sticky=E)
        self.get = Entry(self)
        self.get.grid(row=2,column=1,sticky=E)


root = Tk()
root.title('gui')
root.geometry('200x100')
app = GUI(root)

root.mainloop()
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: Questions about creating a GUI

Postby Larz60+ » Thu Aug 25, 2016 10:42 pm

Hello,

In the previous post i mentioned
also if the widget in column 1 is not active, it will look as though yours starts in column 1. This can be difficult to grasp.


The same applies to rows. so even if you put your button in row 20, there's no guarantee that will place it on the bottom.
The best way to do this is use frames as containers, and place the button in the bottom frame.

Reading the references that I gave you will go a long way to understanding how to do this.

For your code, the following should suffice:
Code: Select all
from tkinter import *


class GUI(Frame):
    def __init__(self, master):
        super(GUI, self).__init__(master)
        self.grid()
        self.objects()
    def gettxt(self):
        self.text = self.get.get()
        print(str(self.text))
        self.num = int(self.text)

    def objects(self):
        self.lbl = Label(self, text = 'words')
        self.lbl.grid(row=0,column=0,sticky=W)
        self.get = Entry(self)
        self.get.grid(row=1,column=1,sticky=W)
        self.b = Button(self, text='click', command=self.gettxt)
        self.b.grid(row=2, column=2, sticky=E)

root = Tk()
root.title('gui')
root.geometry('200x100')
app = GUI(root)

root.mainloop()

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Questions about creating a GUI

Postby LavaCreeperKing » Fri Aug 26, 2016 8:06 pm

Thank you.
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: Questions about creating a GUI

Postby LavaCreeperKing » Sat Aug 27, 2016 4:47 pm

One last question for now. Is it possible for me to make a program that allows me to create a GUI and add things to it then drag and drop them then to create the screen for my GUI? For example I have created a program that allows me to do this for pygame. Or is there some other program I could try. I tried PyQt once but when I tried to get the code for the screen it said it was unable to run its compiler code thing and I could never get it to work.
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: Questions about creating a GUI

Postby Larz60+ » Sat Aug 27, 2016 9:38 pm

Hello,

I haven't ever tried to do this.
The capability may be in ttk, I'm not sure, so you will have to search the ttk documentation to find out,
it's located here: http://wiki.tcl.tk/14796
if it is, you may be able to use the tkinter wrapper tkinter.ttk and use drag and drop from there.

Here's an article that does it with objects on a canvas: http://stackoverflow.com/questions/15466469/tkinter-drag-and-drop

Sorry I'm not more helpful here, but it's not something that I've done

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Questions about creating a GUI

Postby LavaCreeperKing » Sun Aug 28, 2016 4:33 pm

Thank you for your help. I will take a look into it and I also might give PyQt another try.
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: Questions about creating a GUI

Postby Larz60+ » Mon Aug 29, 2016 9:16 am

Hello,

Here's a link to example for PyQt: http://zetcode.com/gui/pyqt4/dragdrop/

I tried this one in python 3.4 on windows 7 - works great

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Questions about creating a GUI

Postby LavaCreeperKing » Mon Aug 29, 2016 5:26 pm

Thank you.
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 4 guests