【问题标题】: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 toPycharm 上没有显示按钮(使用 Tkinter 模块)?这是我的代码,但我似乎无法按我的意愿编辑和格式化按钮
【发布时间】:2020-07-09 05:01:08
【问题描述】:
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()

【问题讨论】:

  • 避免将控制台输入与 GUI 应用程序混合。 “我似乎无法按照我的意愿编辑和格式化按钮” 是什么意思?

标签: python button tkinter pycharm


【解决方案1】:

在您的代码中进行以下更改以成功运行它 -

import tkinter as tk (not with capital T)

if user_input=='1': 

elif user_input=='2':

默认情况下,input() 函数会将其接收到的所有信息转换为字符串。所以你已经将输入与字符串版本进行了比较。

【讨论】:

  • OP 使用的是 Python 2(因为使用了 Tkinter),如果输入“1”,input() 将返回整数 1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 2021-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
相关资源
最近更新 更多