我正在为一个秘密消息项目。我正在尝试使代码在对消息进行加密之后重复其自身。这是我尝试的代码。
from tkinter import Tk, messagebox
print('To decrypt do -(number you used to encryt)')
root = Tk()
alphabet = 'abcdefghijklmnopqrstuvwxyz'
newMessage = ''
message = input('Please enter a message: ')
key = input('Enter a key (-26-26): ')
key = int(key)
for character in message:
if character in alphabet:
position = alphabet.find(character)
newPosition = (position + key) % 26
newCharacter = alphabet[newPosition]
newMessage += newCharacter
else:
newMessage += character
print('Your new message is: ', newMessage)
root.mainloop()
我尝试移动root = Tk()
,但没有成功。另一件事是我不想重复print('To decrypt do -(number you used to encryt)')
如果有人知道为什么不循环,请告诉我。
答案 0 :(得分:2)
Tkinter是一个用python创建GUI应用程序的模块,它与您要尝试执行的操作无关。
在这种情况下,while循环是更好的选择:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
message = ''
while True:
newMessage = ''
print('To decrypt do -(number you used to encryt)')
message = input('Please enter a message: ')
key = input('Enter a key (-26-26): ')
key = int(key)
for character in message:
if character in alphabet:
position = alphabet.find(character)
newPosition = (position + key) % 26
newCharacter = alphabet[newPosition]
newMessage += newCharacter
else:
newMessage += character
print('Your new message is: ', newMessage)
while True
意味着永远继续。您通常希望有一种退出循环的方法。例如,使用while message != "-1"
,这将在用户输入-1
答案 1 :(得分:0)
您将要使用new Thread(new Runnable() {
@Override
public void run() {
while (start_stop) {
runOnUiThread(new Runnable() {
@Override
public void run() {
my_text.setText(Integer.toString(counter));
}
});
counter++;
try {
Thread.sleep(1000); // sleep 1 sec
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
循环。 while
运行图形程序,它不执行您在此处尝试的操作。