【问题标题】:How do I make my program Print the H variable after Hello [duplicate]如何让我的程序在你好之后打印 H 变量 [重复]
【发布时间】:2021-11-19 21:35:41
【问题描述】:
from tkinter import *
root = Tk()

root.title("Matchstick Game")
root.iconbitmap("match.ico")
root.geometry("1000x580")

H = Entry(root, width = 80, fg = 'red').pack()
def clickme():
mylabel = Label(root, text = "Hello" + H.get()).pack()

my_button = Button(root, text = "whats your name", 
padx=10,pady=10,bg='white',fg='green',command=clickme).pack()

root.mainloop()

我收到此错误:AttributeError: 'NoneType' object has no attribute 'get'

【问题讨论】:

    标签: python python-3.x tkinter tkinter-entry


    【解决方案1】:

    Entrypack() 函数返回 None,因此您将 None 存储在 H. 您要做的是将Entry对象存储在H中,然后在其上调用pack方法。

    H = Entry(root, width = 80, fg = 'red')
    H.pack()
    

    【讨论】:

      【解决方案2】:

      cmets 中的 Jason Harper 给出了正确答案。将 .pack() 语句与标签和条目定义分开将修复错误。

      这是我得到的

      import tkinter as tk
      
      root = tk.Tk()
      
      root.title("Matchstick Game")
      #root.iconbitmap("match.ico")
      root.geometry("1000x580")
      
      H = tk.Entry(master = root, width = 80, fg = 'red')
      H.pack()
      
      def clickme():
          mylabel = tk.Label(root, text = ( "Hello" + H.get()))
          mylabel.pack()
      
      my_button = tk.Button(root, text = "whats your name", 
      padx=10,pady=10,bg='white',fg='green',command=clickme)
      
      my_button.pack()
      
      root.mainloop()
      

      希望有所帮助。

      【讨论】:

        猜你喜欢
        • 2020-12-10
        • 2012-04-26
        • 2014-03-20
        • 2019-04-19
        • 1970-01-01
        • 2016-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多