简体   繁体   English

带有 tkinter 的 GUI

[英]GUI with tkinter

Getting started with GUI with Tkinter but it's not running使用 Tkinter 开始使用 GUI,但它没有运行

from tkinter import *

root = Tk()
thelabel = Label(root, "hello")
thelabel.pack()
root.mainloop()

I'm getting the following error:我收到以下错误:

Traceback (most recent call last):
  File "guidemo1.py", line 4, in <module>
    thelabel = Label(root, "hello")
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2766, 

in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2295, in __init__
    classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)]

AttributeError: 'str' object has no attribute 'items'

The documentation for Label says that the second argument is a list not a string. Label文档说第二个参数是一个列表而不是一个字符串。 You could skip this second positional argument and use the keyword argument text :您可以跳过第二个位置参数并使用关键字参数text

thelabel = Label(root, text = "hello")

Instead of代替

thelabel = Label(root, "hello")

You should have used the "text" argument for Labels您应该为标签使用“文本”参数

theLabel = Label(root, text="hello")

the Label is not done correctly, the code must be: Label没有正确完成,代码必须是:

import tkinter

root = tk.Tk()
thelabel = tkinter.Label(root, text="hello")
thelabel.pack()
root.mainloop()

also, you can use from tkinter import* as well, i just do it like this, if you do so, also change the label to Label(root, text="hello") and root to just Tk()此外,您也可以使用from tkinter import* ,我就是这样做的,如果您这样做,也将标签更改为Label(root, text="hello")并将 root 更改为Tk()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM