我试图在我的tktinter python项目中获取一个复选框的值,但是每次我尝试打印该值时,它都会空白。请帮助,谢谢。
((我制作了另一个标签,上面印有'值应该在上面',以确保每次调用函数时标签本身都在印刷。
这里是代码
from tkinter import *
from tkinter.ttk import *
import tkinter as tk # python 3
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage,):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
frame.winfo_toplevel().geometry("1024x720")
frame.configure(bg='#333130')
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
c = Canvas(self, height=50, width=102400, bg="#333130")
c.pack()
newtaskbutton = tk.Button(self, text='New Task', command=lambda: taskcreator())
newtaskbutton_window = c.create_window(600, 12.5, anchor=tk.NE, window=newtaskbutton)
def taskcreator():
global screen
screen = Tk()
screen.geometry('720x640')
var = StringVar()
option1 = tk.Checkbutton(screen, text="Size 11", variable=var, onvalue="Yes", offvalue="No")
option1.deselect()
option1.pack()
you = Button(screen, text='check value', command=lambda: assign(var))
you.place(x=100, y=100)
def assign(var):
theoption = var.get()
mylabel = tk.Label(screen, text=theoption)
mylabel.pack()
alabel = tk.Label(screen, text="The value should be above")
alabel.pack()
app = SeaofBTCapp()
app.mainloop()
问题是您正在创建Tk
的两个实例。第一个是您创建SeaofBTCapp
的实例时,第二个是您执行screen = Tk()
的位置。