簡體   English   中英

帶有 tkinter 的 GUI

[英]GUI with tkinter

使用 Tkinter 開始使用 GUI,但它沒有運行

from tkinter import *

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

我收到以下錯誤:

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'

Label文檔說第二個參數是一個列表而不是一個字符串。 您可以跳過第二個位置參數並使用關鍵字參數text

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

代替

thelabel = Label(root, "hello")

您應該為標簽使用“文本”參數

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

Label沒有正確完成,代碼必須是:

import tkinter

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

此外,您也可以使用from tkinter import* ,我就是這樣做的,如果您這樣做,也將標簽更改為Label(root, text="hello")並將 root 更改為Tk()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM