简体   繁体   English

Pycharm 上没有显示按钮(使用 Tkinter 模块)? 这是我的代码,但我似乎无法按我的意愿编辑和格式化按钮

[英]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.

相关问题 我似乎无法在我的代码中修复这些错误 - I cant seem to fix these erros in my code 我想创建一个tkinter Python按钮,当我按下一个按钮时可以激活多个命令 - I would like to create a tkinter Python button that activates more than one command when I press one button 我无法使用tkinter调整图像大小并显示 - I Cant get an image to be resized and displayed using tkinter 我无法在闲置或pycharm或任何平台中运行python代码 - I cant run my python code in idle or pycharm or any platform 我的乌龟代码有一个错误,我似乎无法解决 - My turtle code has an error i cant seem to fix at the end of it 为什么我的 tkinter 按钮没有显示,我是否在模块中安装了所有东西,如果没有,我该如何安装? - Why is my tkinter button not displaying, have I installed everything in the module if not how can I install it? 我如何每次在 Tkinter 中访问不同的按钮? - How would I access a different button each time in Tkinter? 我无法在 pycharm 或 CMD 上下载模块“pandas” - I cant download module "pandas" on pycharm or CMD 尽管使用了诸如打包、网格等各种技术,但我在 tkinter 上的按钮不会显示……我正在尝试使用“应用”按钮来设置值 - My button on tkinter won't show despite using all sorts of techniques like pack, grid etc… I am trying to use an 'apply' button to set value 如何使用 tkinter 模块使用在 Python 的“条目”中输入的变量创建一个按钮来执行和显示计算? - How do I make a button to execute and display calculation using variables entered in “Entry” in Python using the tkinter module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM