如何从tkinter运行Python脚本?

2024-09-21 09:49:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的朋友制作一个应用程序的图形用户界面。它编码一个字符串。不过,这无关紧要。 我需要使用tkinter来制作它,所以如果你按下一个按钮,它会运行以下代码:

print "Welcome to the encoder"
print "[DO NOT USE CAPITAL LETTERS]"
# define the dictionary
encoder = {"a":"1", "b":"2", "c":"3", "d":"4", "e":"5", "f":"6", 
"g":"7", "h":"8", "i":"9", "j":"10", "k":"11", "l":"12", "m":"13", 
"n":"14", "o":"15", "p":"16", "q":"17", "r":"18", "s":"19", "t":"20", 
"u":"21", "v":"22", "w":"23", "x":"24", "y":"25", "z":"26"}

# take your input
Letter1 = raw_input ("Please input the first letter of the word: ")
Letter2 = raw_input ("Please input the second letter of the word: ")
Letter3 = raw_input ("Please input the third letter of the word: ")
Letter4 = raw_input ("Please input the fourth letter of the word: ")
Letter5 = raw_input ("Please input the fifth letter of the word: ")
Letter6 = raw_input ("Please input the sixth letter of the word: ")
Letter7 = raw_input ("Please input the seventh letter of the word: ")
Letter8 = raw_input ("Please input the eigth letter of the word: ")
Letter9 = raw_input ("Please input the ninth letter of the word: ")
Letter10 = raw_input ("Please input the tenth letter of the word: ")
Letter11 = raw_input ("Please input the eleventh letter of the word: ")
Letter12 = raw_input ("Please input the twelvth letter of the word: ")

# print out the encoded version
print encoder[Letter3] + " " + encoder[Letter2] + " " + encoder[Letter1] + "  " + encoder[Letter6] + " " + encoder[Letter5] + " " + encoder[Letter4] + "  " + encoder[Letter9] + " " + encoder[Letter8] + " " + encoder[Letter7] + "  " + encoder[Letter12] + " " + encoder[Letter11] + " " + encoder[Letter10]  

Tags: oftheencoderinputrawwordprintplease
1条回答
网友
1楼 · 发布于 2024-09-21 09:49:10

你应该写一个函数

encoder = {"a":"1", "b":"2", "c":"3", "d":"4", "e":"5", "f":"6", 
"g":"7", "h":"8", "i":"9", "j":"10", "k":"11", "l":"12", "m":"13", 
"n":"14", "o":"15", "p":"16", "q":"17", "r":"18", "s":"19", "t":"20", 
"u":"21", "v":"22", "w":"23", "x":"24", "y":"25", "z":"26"}

def encode(letters):
    return " ".join(str(encoder.get(c, "")) for c in letters)

然后不管你写什么tkinter代码,用字母列表或小写字符串调用这个函数

Taking input from the user in Tkinter

有关设置tkinter标签的信息,请参见this answer

相关问题 更多 >

    热门问题