LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2008, 06:42 AM   #1
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Rep: Reputation: 30
basic python entry tkinter widget how-to


I'm trying to use entry to get a response:

Code:
from Tkinter import *

root = Tk()
master = Tk()
parent = Tk()

e = Entry(master)
e.pack()
e.focus_set()

def callback():
        answer = e.get()
        print answer
        return answer

b = Button(master, text="get", width=10, command=callback)
b.pack()
generates the widget but I would like to store the response as a variable (possibly keep track of responses eventually). This isn't working out so far.

1. What do I need to do?
2. When I do get the answer (printed, not as a variable), it comes with a new line, how do I eliminate the new line, or get it to signal enter?
 
Old 03-24-2008, 03:56 PM   #2
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
DISCLAIMER: I'm still putzing around with Python, so don't take my answers as gospel.

You could do something like this...
Code:
from Tkinter import *

root = Tk()
master = Frame(root)
master.pack()

e = Entry(master)
e.pack()
e.focus_set()

answer = ""

def setanswer():
  global answer
  answer = e.get()

def printanswer():
  global answer
  print answer

b1 = Button(master, text="SET", width=10, command=setanswer)
b1.pack()

b2 = Button(master, text="PRINT", width=10, command=printanswer)
b2.pack()

root.mainloop()
I set the answer variable as a blank string, then in the functions, I called the global answer variable. It jumps the namespace barrier to get the higher-up variable.

In my modification, the setanswer function sets the variable to whatever is typed into the entry box. The printanswer function prints what is in the answer variable. You can type something new into the entry box, but until you click SET, the variable doesn't change.

I also set master to a frame, and got rid of parent. You were creating three root windows with the three "= Tk()" lines.

Python normal ends a print command with a newline, unless you append a comma at the end...
Code:
print answer,
You can also format your print line, something like...
Code:
print 'The answer is "%s".' % answer
 
Old 03-30-2008, 10:36 AM   #3
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Original Poster
Rep: Reputation: 30
Well, I've been piddling around with this program for a while. I got the program down a while ago (Make a guess, check it, I even got it printing to tkinter), but I can't get it to enter with tkinter.

Thoughts?

Code:
#! /usr/bin/env python

import cinco
import gtk
import Tkinter
from Tkinter import *


correct = cinco.a
correct2 = cinco.word
correct3 = correct2.lower()
correct4 = list(correct3)
reply = []
score = 0
answer = ""
fixed = list(answer)

root = Tk()
master = Frame(root)
master.pack()


e = Entry(master)
e.pack()
e.focus_set()

w = Label(root, text="Lingo Cinco \n A Guessing Game with Five letters and no repeats")
w.pack()

w = Label(root, text="your guess        |       points")
w.pack()



def setanswer():
        global answer
        answer = e.get()

def printanswer():
        global answer
        print answer

b1 = Button(master, text="SET", width=10, command=setanswer)
b1.pack()

b2 = Button(master, text="PRINT", width=10, command=printanswer)
b2.pack()

def IsInt( str ):
        try:

num = int(str)
                return 1
        except ValueError:
                return 0


def checkscore( str ):
        check = list(str)
        s1 = set(correct4)
        s2 = set(check)
        s3 = set.intersection(s1 , s2)
        s4 = len(s3)
        s5 = 0
        s6 = 0
        s7 = 0
        s8 = 0
        s9 = 0
        s10 = 0
        int(s4)
        if check[0] == correct4[0]:
                s6 = 10
        if check[1] == correct4[1]:
                s7 = 10
        if check[2] == correct4[2]:
                s8 = 10
        if check[3] == correct4[3]:
                s9 = 10
        if check[4] == correct4[4]:
                s10 = 10
        return s4 + s5 + s6 + s7 + s8 + s9 + s10

setanswer()
printanswer()
while fixed != correct4:

        if len(answer) != 5 and answer == 'stop':
                w = Label(root, text="Good Bye!")
                w.pack()
                break
        elif len(answer) != 5 and answer == 'quit':
                w = Label(root, text="Good Bye!")
                w.pack()
                break
        elif len(answer) != 5 and answer == 'exit':
                w = Label(root, text="Good Bye!")
                w.pack()
                print "Good Bye!"
                break
        elif len(answer) != 5:
 w = Label(root, text="Your answer must be five letters long with no duplicates!")
                w.pack()
        elif len(answer) == 5:
                if IsInt(reply) == 1:
                        w = Label(root, text="Why would you type a number?")
                        w.pack()
                        print "Why would you type a number?"

                else:
                        fixed = list(answer)
                        newscore = checkscore(fixed)
                        print fixed, correct4, newscore
                        george = [answer, newscore]
                        w = Label(root, text= george)
                        w.pack()
                        #w = Label(root, text= correct4)
                        #w.pack()
                        #w = Label(root, text= newscore)
                        #w.pack()
        else:
                 print "Your answer must be five letters long with no duplicates!"


while fixed == correct4:
        print "good job!"

root.mainloop()
 
  


Reply

Tags
basic, entry, howto, python, tkinter



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in basic button response program in Python 2.4 with the Tkinter module jojotx0 Programming 1 05-23-2006 07:43 PM
Python and Tkinter 1337 Twinkie Fedora 2 08-03-2004 11:35 AM
Tkinter Attributeerror : Entry has no __call__ method jeffsouza Programming 0 02-05-2004 07:57 AM
Python: Tkinter. Chu Programming 0 11-10-2003 01:56 AM
Python and Tkinter Error Gerardoj Programming 1 11-08-2003 09:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration