thisソース:xとyの両方のスクロールバーをtkinterのText()ウィジェットに追加できます。手続きの方法で作業コードは次のとおりです。tkinterのTextウィジェットにxscrollbarを追加する方法
from tkinter import *
root = Tk()
frame = Frame(master, bd=2, relief=SUNKEN)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
xscrollbar.grid(row=1, column=0, sticky=E+W)
yscrollbar = Scrollbar(frame)
yscrollbar.grid(row=0, column=1, sticky=N+S)
text = Text(frame, wrap=NONE, bd=0,
xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
text.grid(row=0, column=0, sticky=N+S+E+W)
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
frame.pack()
root.mainloop()
はしかし、私は動作しませんスクロールバーの作品が、xのスクロールバーyの以下のコードによると、クラスメソッドを選択し、以下のコードを書きました。この例でxスクロールバーが動作しないのはなぜですか?
import tkinter as tk
class App(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.x_scrollbar = tk.Scrollbar(master=self, orient="horizontal")
self.x_scrollbar.grid(row=1, column=0, sticky="w, e")
self.y_scrollbar = tk.Scrollbar(master=self)
self.y_scrollbar.grid(row=0, column=1, sticky="n, s")
self.text = tk.Text(master=self, width=100, height=25, bg="black", fg="white", wrap=None)
self.text.grid(row=0, column=0, sticky="n, s, e, w")
self.configure_widgets()
self.pack()
def configure_widgets(self):
self.text.configure(xscrollcommand=self.x_scrollbar.set, yscrollcommand=self.y_scrollbar.set)
self.x_scrollbar.config(command=self.text.xview)
self.y_scrollbar.config(command=self.text.yview)
if __name__ == "__main__":
root = tk.Tk()
app = App(master=root)
app.mainloop()
から
*
をインポートするときはちょうどここに参考のためにあなたが得る
'nsew'
定数の一覧です。 –また、私は粘着性のある名前を変更しました、もう一度ありがとうございます。 –
喜んで助けてください。この回答がうまくいったら、私の答えの横のチェックボックスを選択してください。 –