简体   繁体   中英

How do I get information out of my entry box to use later in my program in Python?

How can get information from my entry point? I'd like to be able to look at my matrix and pick certain input entries from my loop to see what is in the entry box. Here is my code so far:

rows = int(input('How many rows does your matrix have?: '))
cols = int(input('How many columns does your matrix have?: '))
win = GraphWin('Matrix', 300,300)
win.setBackground('white')
total = 1
for i in range(rows):
    y = 75 + 40*i
    for k in range(cols):
        x = 50 + 50*k
        entry = Entry(Point(x,y),3)
        entry.draw(win)
        k
print(entry.getText())

The problem is that you are reusing your entry variable so the only Entry you have a pointer to is the last one. We need to keep a list of Entry instances so we can poll them:

from graphics import *

rows = int(input('How many rows does your matrix have?: '))
cols = int(input('How many columns does your matrix have?: '))

win = GraphWin('Matrix', cols * 60, rows * 60)

entries = []

for i in range(rows):
    y = 75 + i*40
    for k in range(cols):
        x = (k + 1) * 50
        entry = Entry(Point(x, y), 3)
        entry.draw(win)

        entries.append(entry)

win.getMouse()
win.close()

for entry in entries:
    print(entry.getText())

Once the interface comes up, click on Entry boxes and enter letters -- don't click elsewhere. When you're done, click on the background of the window and the interface should go away and print the contents of all the Entry boxes to the console. Rework as your needs demand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM