簡體   English   中英

從tkinter.ttk導入*干擾復選框列表

[英]from tkinter.ttk import * interfering with check box list

我已經from tkinter.ttk import *導入了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("")

這就是為什么您不應該進行通配符導入的原因,尤其是對於tkinter和ttk。 它們都導出具有相同名稱的對象。

最好的解決方案是更改導入,然后更改代碼以使其匹配。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM