LinuxとWindowsでEntryウィジェットのサイズを指定する際に問題があります。これらのエントリはLinuxで作成されました:
http://i56.tinypic.com/2edaaeg.png
Linuxでは、Textウィジェット内で見栄えがよくなります。次のコード行で作成された2つのEntryセルが1つずつあります。
tk.Entry(master、width = 16)
幅は16文字で指定します。
しかし、Windowsでは、セルはスペースの半分しか占有せず、フォントサイズがWindows上ではより小さくなるため、幅を22に指定する必要があります。
私の質問です:テキストウィジェットでこれらの2つのセルの相対的な幅を指定する方法があるので、各セルは親ウィジェットの1/2を取る?
Within a text widget? No, there is no direct support for relative widths. within a frame? yes. If you are putting them in a text widget (I presume, so you can scroll them) you have to manage the widths yourself. You can add a binding to the
event of the text widget. This fires when the text widget changes size, and you can resize all the widgets at that point.
最も簡単な方法は、 grid
を使用してフレームに配置し、フレームをキャンバスに置き、スクロールできるようにすることです。
ここに例があります:
import Tkinter as tk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.canvas = tk.Canvas(self, width=200, highlightthickness=0)
self.vsb = tk.Scrollbar(orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.canvas.pack(side="left", fill="both", expand=True)
self.container = tk.Frame(self.canvas, borderwidth=0, highlightthickness=0)
self.container.grid_columnconfigure(0, weight=1)
self.container.grid_columnconfigure(1, weight=1)
for i in range(30):
e1 = tk.Entry(self.container)
e2 = tk.Entry(self.container)
e1.grid(row=i, column=0,sticky="ew")
e2.grid(row=i, column=1,sticky="ew")
e1.insert(0, "find %s" % i)
e2.insert(0, "replace %s" % i)
self.canvas.create_window((0,0), anchor="nw", window=self.container, tags="container")
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
self.canvas.bind("", self.OnCanvasConfigure)
def OnCanvasConfigure(self, event):
self.canvas.itemconfigure("container", width=event.width)
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
if __name__ == "__main__":
app = SampleApp()
app.mainloop()