私はウィジェット全体を意味し、内部のテキストではありません。また、フォーラムで既存のソリューションを入手しようとしました。問題のみを表示するためにコードを削除しました。
私の問題は...ウィジェットをに配置するComboBox
と、Entry
ウィジェットが。でcanvas2
削除されないことcanvas2.delete("all")
です。
メニューから「加重グラフ」を選択し、次に「無向グラフ」を選択すると、問題が発生します。「加重グラフ」の後ComboBox
、Entry
ウィジェットとウィジェットは残ります(他のすべては期待どおりに削除されます)。私は時間遅延と問題を示すためにいくつかのプロンプトを入れました。
from tkinter import *
from tkinter import ttk
import time
tk = Tk()
tk.geometry("970x660")
menubar = Menu(tk)
tk.config(menu=menubar)
wrapper1 = LabelFrame(tk, text="Display", height=500)
wrapper2 = LabelFrame(tk, text="Command", width=800)
# canvas was setup to be a vertically scrolling region, I have cut most of that code
# (but my problem does not lie with canvas, that's working fine)
canvas = Canvas(wrapper1, height=500)
canvas.pack(side=LEFT, fill="both", expand="yes")
myframe = Frame(canvas)
canvas.create_window((0,0), window=myframe, anchor="nw", height=500)
# Note: My problem lies on canvas2,
canvas2 = Canvas(wrapper2)
canvas2.pack(side=LEFT, fill="both", expand="yes")
canvas2.update()
wrapper1.pack(fill="both", expand="yes", padx=10, pady=0)
wrapper2.pack(fill="both", expand="yes", padx=10, pady=5)
def DoNothing():
print("Doing nothing!")
def BinaryTree(Weighted, Mode):
canvas.delete("all")
print("Before canvas2.delete...")
canvas2.delete("all")
print("...after canvas2.delete... now waiting 3 seconds so you can inspect if canvas2 is CLEAR... ")
print("...I would expect canvas2 to be clear but Combobox and Entry remain!... ")
canvas2.update()
tk.update()
time.sleep(3)
button1 = Button(canvas2, text = "Add Node", command= lambda: DoNothing(), width=13, anchor=W)
button1_window = canvas2.create_window(20, 40, anchor=W, window=button1)
entry1 = Entry(canvas2, width=10)
entry1.place(x=150, y=40, anchor=W)
button2 = Button(canvas2, text = "Add Edge", command= lambda: DoNothing(), width=13, anchor=W)
button2_window = canvas2.create_window(20, 80, anchor=W, window=button2)
cb1 = ttk.Combobox(canvas2, values=DoNothing(), postcommand=lambda: DoNothing(), width=13, state="readonly")
cb1.place(x=150, y=80, anchor=W)
cb2 = ttk.Combobox(canvas2, values=DoNothing(), postcommand=lambda: DoNothing(), width=13, state="readonly")
cb2.place(x=260, y=80, anchor=W)
if Weighted == True:
canvas2.create_text(380, 80, text="Weight:", anchor=W)
entry2 = Entry(canvas2, width=4)
entry2.place(x=429, y=80, anchor=W)
button3 = Button(canvas2, text = "Delete All Nodes", command= lambda: DoNothing(), width=13, anchor=W)
button3_window = canvas2.create_window(420, 40, anchor=W, window=button3)
canvas2.update()
tk.update()
lmenu = Menu(menubar)
menubar.add_cascade(label="Learning Menu", menu=lmenu)
lmenu.add_command(label="Weighted Graph", command= lambda: BinaryTree(True, "DirectedGraph"))
lmenu.add_command(label="Undirected Graph", command= lambda: BinaryTree(False, "UndirectedGraph"))
while True:
tk.update_idletasks()
tk.update()
time.sleep(0.01)
コンボボックスウィジェットとエントリウィジェットがdelete( "all")によって削除されないのはなぜですか?
それがtkinterが機能するように設計されている方法です。canvasdelete
メソッドは、canvasオブジェクトのみを削除します。ことを意味し、「作成」機能(とキャンバスの上に作成されたオブジェクトcreate_rectangle
、create_window
など)。ウィンドウオブジェクトの場合、delete
メソッドはで作成されたキャンバスオブジェクトcreate_window
を削除しますが、そのウィンドウオブジェクトに関連付けられているウィジェットを削除するようには設計されていません。
他のウィジェットと同様に、実際のウィジェットを削除するにはdestroy
、ウィジェット自体のメソッドを呼び出す必要があります。
私の問題は... ComboBoxウィジェットとEntryウィジェットをcanvas2に配置すると、canvas2.delete( "all")で削除されません。
これは、を使用する場合place
、キャンバスオブジェクトを作成していないためです。以下のためにdelete
仕事へのコマンドは、呼び出す必要がありcreate_window
COMBOXエントリウィジェットのために。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加