3 Replies - 479 Views - Last Post: 05 May 2013 - 01:57 PM Rate Topic: -----

#1 Highest bidder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 23-November 12

Question for GUI in python

Posted 05 May 2013 - 03:49 AM

i am trying to make a program that creates a GUI with several buttons. Once you press one of the buttons i want the program to write the assigned value on an Entry field. How can i make that possible?

for example i want something like this:

import Tkinter

window = Tkinter.Tk()

button_1 = Tkinter.Button(window, activebackground = "Red", width = 2, height = 1, text = '1', cursor = "Plus", command = insert_one)

in_field2 = Tkinter.Entry(window, bd = 3)
in_field2.grid(row = 0, column = 1)
in_field2.place(x = 100, y = 280)
in_field2.focus_set()






now i want to run the inster_one function that will give the a value to the in_field2

This post has been edited by Highest bidder: 05 May 2013 - 03:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question for GUI in python

#2 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Question for GUI in python

Posted 05 May 2013 - 10:43 AM

You would use a Tkinter variable, either a BooleanVar, DoubleVar, IntVar, or StringVar) and get or set in the usual manner as most widgets can utilize a Tkinter variable. Also be aware that you should not mix pack(), grid(), or place() in the same program because the result is unpredictable.

This post has been edited by woooee: 05 May 2013 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 Highest bidder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 23-November 12

Re: Question for GUI in python

Posted 05 May 2013 - 12:19 PM

View Postwoooee, on 05 May 2013 - 10:43 AM, said:

You would use a Tkinter variable, either a BooleanVar, DoubleVar, IntVar, or StringVar) and get or set in the usual manner as most widgets can utilize a Tkinter variable. Also be aware that you should not mix pack(), grid(), or place() in the same program because the result is unpredictable.


Thanks for your answer but let me make it a bit more clear. My problem is that i have two different Entry fields, and i want to be able to choose with my cursor which one to use. I have done some more research since i posted my question and i think i have to use either the focus_get() or the focus_displayof() command, but i cant figure out how to use them... Do you have any suggestions about that?
Was This Post Helpful? 0
  • +
  • -

#4 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Question for GUI in python

Posted 05 May 2013 - 01:57 PM

Quote

now i want to run the inster_one function that will give the a value to the in_field2
By default you can pick either one with the cursor and enter data. But what happens when the button is pushed? The function can get the contents of both Entrys and see which one contains data, or you can have two buttons, one for each Entry. I don't understand your question well enough to answer further. If you want to know when anything is entered you can bind <KeyPress> as in the following.
try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

from functools import partial

def key_in(entry_num, event):
    print "\nEntry number", entry_num,
    ##shows key or tk code for the key
    if event.keysym == 'Escape':
        root.quit()
    if event.char == event.keysym:
        # normal number and letter characters
        print'Normal Key', event.char
    elif len(event.char) == 1:
        # charcters like []/.,><#$ also Return and ctrl/key
        print( 'Punctuation Key %r (%r)' % (event.keysym, event.char) )
    else:
        # f1 to f12, shift keys, caps lock, Home, End, Delete ...
        print( 'Special Key %r' % event.keysym )

root = tk.Tk()
tk.Label(root, text="Press a key (Escape key to exit):" ).grid()

for ctr in range(2):
    ent=tk.Entry(root)
    ent.bind('<KeyPress>', partial(key_in, ctr+1))
    ent.grid(row=ctr+1, column=0)
    ent.focus_set()

root.mainloop() 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1