python网格不允许get()

2024-04-25 08:41:36 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行下面的代码并打开GUI时,当我在输入框中输入内容并按下按钮时,它会显示下面提到的错误。 但是当我使用place而不是grid时,它工作得很好。 我也在使用pyqr码库

有办法解决这个问题吗

import tkinter
import pyqrcode
from multiprocessing import Process, Queue
from pyqrcode import *
from tkinter import *
from tkinter import messagebox
from tkinter import Tk, Label, Button, Entry
class app:
    def __init__(self, master):
        self.master = master
        master.title("Prosmack qr")
        self.L1=Label(master, text="Enter the qr content").grid(row=0, column=1, columnspan =2)
        self.E1=Entry(master, text="Enter the qr content").grid(row=0,column=3)
        self.B1=Button(master, text="Save as SVG" ,command = self.message1).grid(row=1, column=1, columnspan =2)
        self.B2=Button(master, text="Save as EPS" ,command = self.message2).grid(row=1, column=3, columnspan =2)
        self.L2=Label(master,text="Copyrights owned by prosmack").grid(row=2, column=1,columnspan=5)
    def message1(self):
        url = pyqrcode.create(self.E1.get())
        print(url.terminal(quiet_zone=1))
        url.svg('uca-url.svg', scale=1)
    def message2(self):
        url = pyqrcode.create(self.E1.get())
        print(url.terminal(quiet_zone=1))
        url.eps('uca-url.eps', scale=2)
root = Tk()
app = app(root)
root.resizable(0,0)
root.mainloop

以下是终端显示的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Home\AppData\Local\Programs\Python\Python36-32\lib\idlelib\run.py", line 137, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "C:\Users\Home\AppData\Local\Programs\Python\Python36-32\lib\queue.py", line 172, in get
    raise Empty
queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Home\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\Home\Documents\New folder\fk.py", line 18, in message1
    url = pyqrcode.create(self.E1.get())
AttributeError: 'NoneType' object has no attribute 'get'

Tags: textinfromimportselfmasterurlget
1条回答
网友
1楼 · 发布于 2024-04-25 08:41:36

.grid()方法在Tkinter中返回None。因此,当您这样做时,例如:

self.L1=Label(master, text="Enter the qr content").grid(row=0, column=1, columnspan =2)

您正在将None分配给self.L1。相反,您需要将Label(或EntryButton)的创建分成两行,如下所示:

self.L1=Label(master, text="Enter the qr content")
self.L1.grid(row=0, column=1, columnspan =2)

相关问题 更多 >