我有一个以前没有遇到过的问题:
我的程序中的解释顺序似乎与我的预期有所不同。我写了一个小型 Twitter 客户端。单击“GO”按钮(也可以通过按键盘上的 ENTER 激活)后,我的程序实际发布推文需要几秒钟。我不想在这段时间内多次点击,以为我没有第一次点击它。因此,当单击按钮时,我希望标签文本显示一些告诉我按钮已被单击的内容。
我通过在发送推文之前更改标签文本来实现此消息。但是,由于某种原因,在尝试发送推文之前不会显示该消息。但是由于我在推文之后有一条确认消息,所以我再也看不到这条消息,我最初的问题也没有解决。
我真的很感激任何帮助。以下是相关代码:
class SimpleTextBoxForm(Form):
def __init__(self):
# set window properties
self.Text = "Tweeter"
self.Width = 235
self.Height = 250
#tweet away
self.label = Label()
self.label.Text = "Tweet Away..."
self.label.Location = Point(10, 10)
self.label.Height = 25
self.label.Width = 200
#get the tweet
self.tweetBox = TextBox()
self.tweetBox.Location = Point(10, 45)
self.tweetBox.Width = 200
self.tweetBox.Height = 60
self.tweetBox.Multiline = True
self.tweetBox.WordWrap = True
self.tweetBox.MaxLength = 140;
#ask for the login ID
self.askLogin = Label()
self.askLogin.Text = "Login:"
self.askLogin.Location = Point(10, 120)
self.askLogin.Height = 20
self.askLogin.Width = 60
self.login = TextBox()
self.login.Text= ""
self.login.Location = Point(80, 120)
self.login.Height = 40
self.login.Width = 100
#ask for the password
self.askPass = Label()
self.askPass.Text = "Password:"
self.askPass.Location = Point(10, 150)
self.askPass.Height = 20
self.askPass.Width = 60
# display password box with character hiding
self.password = TextBox()
self.password.Location = Point(80, 150)
self.password.PasswordChar = "x"
self.password.Height = 40
self.password.Width = 100
#submit button
self.button1 = Button()
self.button1.Text = 'Tweet'
self.button1.Location = Point(10, 180)
self.button1.Click += self.update
self.AcceptButton = self.button1
#pack all the elements of the form
self.Controls.Add(self.label)
self.Controls.Add(self.tweetBox)
self.Controls.Add(self.askLogin)
self.Controls.Add(self.login)
self.Controls.Add(self.askPass)
self.Controls.Add(self.password)
self.Controls.Add(self.button1)
def update(self, sender, event):
if not self.password.Text:
self.label.Text = "You forgot to enter your password..."
else:
self.tweet(self.tweetBox.Text, self.login.Text, self.password.Text)
def tweet(self, msg, login, password):
self.label.Text = "Attempting Tweet..." # this should be executed before sending the tweet is attempted. But this seems to be executed only after the try block
try:
success = 'Tweet successfully completed... yay!\n' + 'At: ' + time.asctime().split()[3]
ServicePointManager.Expect100Continue = False
Twitter().UpdateAsXML(login, password, msg)
except:
error = 'Unhandled Exception. Tweet unsuccessful'
self.label.Text = error
else:
self.label.Text = success
self.tweetBox.Text = ""