LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   basic python entry tkinter widget how-to (http://www.linuxquestions.org/questions/programming-9/basic-python-entry-tkinter-widget-how-to-630201/)

donnied 03-24-2008 06:42 AM

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?

ranger_nemo 03-24-2008 03:56 PM

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

donnied 03-30-2008 10:36 AM

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()



All times are GMT -5. The time now is 07:21 PM.