我正在嘗試制作一個GUI,該GUI具有兩個單獨的文本輸出,每個文本框都有水平和垂直的scollbar,這些文本框固定在每個文本窗口的右邊緣和下邊緣。我正在努力解決如何使用tkinter網格做到這一點,任何幫助將不勝感激。import tkinter as tkclass WeatherGUI(tk.Tk): def __init__(self): tk.Tk.__init__(self) # Horizontal (x) Scroll bar self.xscrollbar = tk.Scrollbar(self, orient="horizontal") self.xscrollbar.grid(column=5, row=10, sticky="we") # Vertical (y) Scroll Bar self.yscrollbar = tk.Scrollbar(self) self.yscrollbar.grid(column=5, row=10, sticky='ns') self.xscrollbar2 = tk.Scrollbar(self, orient="horizontal") self.xscrollbar2.grid(column=9, row=10, sticky="we") # Vertical (y) Scroll Bar self.yscrollbar2 = tk.Scrollbar(self) self.yscrollbar2.grid(column=9, row=10, sticky='ns') self.NSW_actual_text = tk.Text(self, width=50, wrap = "none", xscrollcommand=self.xscrollbar.set, yscrollcommand=self.yscrollbar.set,) self.NSW_actual_text.grid(column=0, columnspan=4, row= 8,padx=(20, 10)) self.NSW_forecast_text = tk.Text(self, width=50, wrap = "none", xscrollcommand=self.xscrollbar.set, yscrollcommand=self.yscrollbar.set) self.NSW_forecast_text.grid(column=8, columnspan=4, row= 8,padx=(20, 10)) self.xscrollbar.config(command=self.NSW_actual_text.xview) self.yscrollbar.config(command=self.NSW_actual_text.yview) self.xscrollbar2.config(command=self.NSW_forecast_text.xview) self.yscrollbar2.config(command=self.NSW_forecast_text.yview) self.btn1 = tk.Button(self, text="Generate NWS Actual", command=self.GenerateNWSActual) self.btn1.grid(column=1, row=0) self.btn2 = tk.Button(self, text="Generate NWS Forecast", command=self.GenerateNWSForecast) self.btn2.grid(column=10, row=0) def GenerateNWSActual(self): self.NSW_actual_text.insert('1.0', "This is where actual weather would go")
1 回答

三國紛爭
TA貢獻1804條經驗 獲得超7個贊
以下示例允許您將兩個功能性滾動條 (x, y) 附加到“文本”構件
from tkinter import *
# Create Window
root = Tk()
# Create ScrollBars
xScrollbar = Scrollbar(root, orient=HORIZONTAL)
yScrollbar = Scrollbar(root, orient=VERTICAL)
# Create Text Widget with scroll commands
TextWidget = Text(root, xscrollcommand=xScrollbar, yscrollcommand=yScrollbar)
# Package Componets
xScrollbar.pack(side=BOTTOM, fill=X)
yScrollbar.pack(side=RIGHT, fill=Y)
TextWidget.pack(fill=BOTH, expand=20)
# Assign Scrollbars with TextWidget
xScollbar.config(command=TextWidget.xview)
yScollbar.config(command=TextWidget.yview)
您可以在天氣應用程序中將此檢查用于兩個文本標記。
添加回答
舉報
0/150
提交
取消