2017-11-21 17 views
1

私のcheckbuttonはボタンで上書きされています。ボタンにx axisy axisを与えるには?tkinterのチェックボタンの上書きを避ける方法

l.pack()の代わりにl.place()を使用しようとしましたが、何も返されません。この上書きを避けるには、l.pack()の代わりに何を使用する必要がありますか?

コード:

checkbuttonべき秒:

import tkinter as tk 
import os 
import tkinter.filedialog 


class ScrolledFrame(tk.Frame): 

    def __init__(self, parent, vertical=True, horizontal=False): 
     super().__init__(parent) 

     self._canvas = tk.Canvas(self) 
     self._canvas.grid(row=0, column=0) 

     self._vertical_bar = tk.Scrollbar(self, orient="vertical", 
command=self._canvas.yview) 
     if vertical: 
      self._vertical_bar.grid(row=0, column=1, sticky="ns") 
     self._canvas.configure(yscrollcommand=self._vertical_bar.set) 

     self._horizontal_bar = tk.Scrollbar(self, orient="horizontal", 
command=self._canvas.xview) 
     if horizontal: 
      self._horizontal_bar.grid(row=1, column=0, sticky="we") 
     self._canvas.configure(xscrollcommand=self._horizontal_bar.set) 

     self.frame = tk.Frame(self._canvas) 
     self._canvas.create_window((0, 0), window=self.frame, anchor="nw") 

     self.frame.bind("<Configure>", self.resize) 

    def resize(self, event=None): 
     self._canvas.configure(scrollregion=self._canvas.bbox("all")) 


#This is not in class  
def call(): 

    # add to scrolled frame 

    data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'] 
    variables = [] 

    # add widgets to scrolled frame - as parent you have to use `sf.frame` instead of `sf` 
    for txt in data: 
     var = tk.IntVar() 
     variables.append(var) 
     l = tk.Checkbutton(sf.frame, text=txt, variable=var) 
     l.pack() 



root = tk.Tk() 

sf = ScrolledFrame(root) 
sf.pack() 


btn1 = tkinter.Button(root, text="Source Path", command = call) 
btn1.place(x = 0,y = 0) 

ent1 = tkinter.Entry(root) 
ent1.place(x = 100,y = 0,width = 200,height=25) 

btn1 = tkinter.Button(root, text="destination") 
btn1.place(x = 0,y = 40) 

ent1 = tkinter.Entry(root) 
ent1.place(x = 100,y = 40,width = 200,height=25) 

root.mainloop() 

誤った出力:出力リレー期待

enter image description here

Destination Buttonの下のタルト。

+0

異なるレイアウト方法を1つのコンテナ内に混ぜてはいけません**。 '.place'メソッドはうまく動作しません。' .grid'を使うことをお勧めします。正しく使うのがずっと簡単です。 –

+0

私は試しましたが(l.grid(sticky = "w"))、私は同じ結果をPM2Ringで取得しています – sam

+0

すべてのウィジェット、checkbuttonsだけでなく 'btn1'と' 'ent1'。これは、ボタンと項目がパックされたチェックボタンと重複して配置されるためです。 –

答えて

1

コメントに記載されているとおり、さまざまなレイアウトを混在させないでください。
次に、作成しているフレームに入力ボックスとボタンがある方がよいでしょう。ここで

はあなたが好む場合は、別のレイアウトに変更することができ、私が提案するものです:あなたがボタンをクリックすると

import tkinter as tk 
import os 
import tkinter.filedialog 

class ScrolledFrame(tk.Frame): 

    def __init__(self, parent, vertical=True, horizontal=False): 
     super().__init__(parent) 

     self._canvas = tk.Canvas(self) 
     self._canvas.grid(row=0, column=0) 

     self._vertical_bar = tk.Scrollbar(self, orient="vertical", command=self._canvas.yview) 
     if vertical: 
      self._vertical_bar.grid(row=0, column=1, sticky="ns") 
     self._canvas.configure(yscrollcommand=self._vertical_bar.set) 

     self._horizontal_bar = tk.Scrollbar(self, orient="horizontal", command=self._canvas.xview) 
     if horizontal: 
      self._horizontal_bar.grid(row=1, column=0, sticky="we") 
     self._canvas.configure(xscrollcommand=self._horizontal_bar.set) 

     self.frame = tk.Frame(self._canvas) 
     self._canvas.create_window((0, 0), window=self.frame, anchor="nw") 

     self.frame.bind("<Configure>", self.resize) 

     btn1 = tkinter.Button(root, text="Source Path", command = call) 
     btn1.pack() 

     ent1 = tkinter.Entry(root) 
     ent1.pack() 

     btn1 = tkinter.Button(root, text="destination") 
     btn1.pack() 

     ent1 = tkinter.Entry(root) 
     ent1.pack() 
     self.pack() 

    def resize(self, event=None): 
     self._canvas.configure(scrollregion=self._canvas.bbox("all")) 


def call(): 
    # add components to scrolled frame  
    data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'] 
    variables = [] 

    # add widgets to scrolled frame - as parent you have to use `sf.frame` instead of `sf` 
    for txt in data: 
     var = tk.IntVar() 
     variables.append(var) 
     l = tk.Checkbutton(sf.frame, text=txt, variable=var) 
     l.pack() 


root = tk.Tk() 
sf = ScrolledFrame(root) 
root.mainloop() 

あなたのダニボックスが表示されます。

+0

ありがとうございました。 – sam

+0

うれしい私は助けることができます。 –

関連する問題