[英]button not being displayed on Pycharm (using the Tkinter module)?. This is my code but I cant seem to edit and format the button as I would like to
import Tkinter as tk
import webbrowser
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
def Alberta():
webbrowser.open_new(r"https://news.google.com/covid19/map?hl=en-CA&mid=%2Fm%2F0j95&gl=CA&ceid=CA%3Aen")
def BC():
webbrowser.open_new(r"https://news.google.com/covid19/map?hl=en-CA&mid=%2Fm%2F015jr&gl=CA&ceid=CA%3Aen")
print(" Enter 1 for Alberta \n Enter 2 for BC \n ")
user_input= input() # type: int
if user_input==1:
lab= tk.Label(root,bg="yellow", text="Alberta")
lab.pack()
button = tk.Button(frame,
fg="red",
command=Alberta)
button.pack(side=tk.LEFT)
elif user_input==2:
button = tk.Button(frame,
fg="red",
command=BC)
lab = tk.Label(root, text="British Columbia")
lab.pack()
button.pack(side=tk.LEFT)
else:
print("Invalid input! Try Again!")
root.geometry('300x400')
root.mainloop()
Make following changes in your code to run it successfully -在您的代码中进行以下更改以成功运行它 -
import tkinter as tk (not with capital T)
if user_input=='1':
elif user_input=='2':
The input() function, by default, will convert all the information it receives into a string. input() function 默认情况下会将其接收到的所有信息转换为字符串。 So you have comparet the input with string version.
所以你已经将输入与字符串版本进行了比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.