Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python 3.x - Inside and outside function

This code is letting me input a string, and that string is added to the dict 'd'. However I want to use this functionality in a gui but when used with the gui I am missing something. Because it won′t work.

This first code works as intended (being outside a function)(Print statements are there just for the testing purpose.):


d = {"username": "XYZ", "email": "[email protected]", "location":"Mumbai"}

r=str(input())
d[r] =' '

print(r)
print(d)

While this code below does not work as intended. When I run the code it just idles. Why? How do I fix this global/local issue (just my guess)?


def newSite():
    print('opening dialogue')
    r = str(input())
    
    d[r] =' '
    

The function is called in a button.

Button(root, height=1, width=10, text=" <- Input site!  ", command=newSite).grid(row=6, column=3, sticky=W, pady=10)  
question from:https://stackoverflow.com/questions/66056800/inside-and-outside-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is not a good idea to call console input() in a GUI application, use simpledialog.askstring() instead:

from tkinter import simpledialog
...

def newSite():
    print("opening dialogue")
    r = simpledialog.askstring("Input", "Enter Site")
    if r and r.strip():
        d[r.strip()] = " "
    print(d)

...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...