【问题标题】:from tkinter.ttk import * interfering with check box list从 tkinter.ttk 导入 * 干扰复选框列表
【发布时间】:2017-08-18 05:12:03
【问题描述】:

我已经导入了from tkinter.ttk import *,所以我的下拉菜单可以正常工作,使用这个模块,下拉菜单不会重复某些选项,并使窗口成为里面文本的宽度。

但是,复选框窗口的模块不起作用,并且只显示一个空白窗口。 我该怎么做才能正常工作。

下拉代码:

import random
from tkinter import *
from tkinter.ttk import *

with open('Comedy films', encoding='latin-1') as f:
    lines = f.readlines()
    value_fifteen_one=(random.choice(lines))
    value_fifteen_two=(random.choice(lines))
    value_fifteen_three=(random.choice(lines))
    value_fifteen_four=(random.choice(lines))
    value_fifteen_five=(random.choice(lines))

def menu_fifteen():      
    root15 = Tk()
    root15.title("Film choices")

    menu = Frame(root15)
    menu.pack(pady = 5, padx = 50)          
    var = StringVar(root15)

    options = [
            value_fifteen_one,
            value_fifteen_two,
            value_fifteen_three,
            value_fifteen_four,
            value_fifteen_five,
    ]
    option = OptionMenu(menu, var, options[0], *options, command=functionsfifteen)
    var.set('Select')
    option.grid(row = 1, column = 1)

    root15.mainloop()


def functionsfifteen(value):
    if value == value_fifteen_one:            
        print("Playing "+value_fifteen_one)
    if value == value_fifteen_two:            
        print("Playing "+value_fifteen_two)
    if value == value_fifteen_three:            
        print("Playing "+value_fifteen_three)
    if value == value_fifteen_four:            
        print("Playing "+value_fifteen_four)
    if value == value_fifteen_five:            
        print("Playing "+value_fifteen_five)

复选框代码:

def interests():
    check = Tk()
    check.title("Interests")

    CheckVar1 = BooleanVar()
    CheckVar2 = BooleanVar()
    CheckVar3 = BooleanVar()
    CheckVar4 = BooleanVar()


    C1 = Checkbutton(check, text = "Horror", variable = CheckVar1, \
                     onvalue = 1, offvalue = 0, height=1, \
                     width = 20)
    C2 = Checkbutton(check, text = "Action", variable = CheckVar2, \
                     onvalue = 1, offvalue = 0, height=1, \
                     width = 20)
    C3 = Checkbutton(check, text = "Science fiction", variable = CheckVar3, \
                     onvalue = 1, offvalue = 0, height=1, \
                     width = 20)
    C4 = Checkbutton(check, text = "Comedy", variable = CheckVar4, \
                     onvalue = 1, offvalue = 0, height=1, \
                     width = 20)

    def submitHandle():
        anyChecked = CheckVar1.get() | CheckVar2.get() | CheckVar3.get(
        ) | CheckVar4.get()
        if anyChecked:
            check.destroy()
        else:
            messagebox.showerror("Error", "Please check at least one.")

    submit_btn = Button(check, text="Submit", command=lambda: submitHandle()) #Submit button on the check button menu window


    C1.pack()
    C2.pack()
    C3.pack()
    C4.pack()
    submit_btn.pack()
    check.mainloop()


    if CheckVar1.get():
        filename = ("Horror");
        with open (filename, "a") as f:
            f.write("")

    if CheckVar2.get():
        filename = ("Action");
        with open (filename, "a") as f:
            f.write("")

    if CheckVar3.get():
        filename = ("Science fiction");
        with open (filename, "a") as f:
            f.write("")

    if CheckVar4.get():
        filename = ("Comedy");
        with open (filename, "a") as f:
            f.write("")

【问题讨论】:

    标签: python python-3.x checkbox tkinter text-files


    【解决方案1】:

    这正是您不应该进行通配符导入的原因,尤其是对于 tkinter 和 ttk。它们都导出具有相同名称的对象。

    最好的解决方案是更改您的导入,然后更改您的代码以匹配。

    import tkinter as tk
    import tkinter.ttk as ttk
    ...
    root15 = tk.Tk()
    ...
    

    【讨论】:

    • 谢谢,快速提问 - 我将 menu 定义为什么,因为 Frame 由于不是任何导入的一部分而未定义。
    猜你喜欢
    • 2015-01-23
    • 2017-09-23
    • 2017-05-28
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多