【发布时间】:2021-11-13 08:28:36
【问题描述】:
我试图用 tkinter 在 python 中创建一个二维码生成应用程序,你输入颜色,然后输入文本,它会生成一个二维码,但它会在启动时立即关闭。 (当我没有颜色部分时,问题没有发生),(将来我还计划添加一个保存QR为png按钮) 这是我所拥有的:
from tkinter import *
from tkinter import messagebox
import pyqrcode
from tkinter import colorchooser
import re
def choose_color():
x = 0
# variable to store hexadecimal code of color
color_code = colorchooser.askcolor(title ="Choose color")
print(color_code)
x = 1
root = Tk()
button = Button(root, text = "Select color",
command = choose_color)
button.pack()
if 'x' == 1:
str = 'color_code' # Your Hex
match = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', str)
if match:
print('Hex is valid')
ws = Tk()
ws.title("PythonGuides")
ws.config(bg='color_code')
def generate_QR():
if len(user_input.get())!=0 :
global qr,img
qr = pyqrcode.create(user_input.get())
img = BitmapImage(data = qr.xbm(scale=8))
else:
messagebox.showwarning('warning', 'All Fields are Required!')
try:
display_code()
except:
pass
def display_code():
img_lbl.config(image = img)
output.config(text="QR code of " + user_input.get())
lbl = Label(
ws,
text="Enter message or URL",
bg='color_code'
)
lbl.pack()
user_input = StringVar()
entry = Entry(
ws,
textvariable = user_input
)
entry.pack(padx=10)
button = Button(
ws,
text = "generate_QR",
width=15,
command = generate_QR
)
button.pack(pady=10)
img_lbl = Label(
ws,
bg='color_code')
img_lbl.pack()
output = Label(
ws,
text="",
bg='color_code'
)
output.pack()
ws.mainloop()
else:
print('Hex is not valid')
【问题讨论】:
标签: python tkinter colors qr-code