我做了 40 年的程序员,但主要是命令行和系统编程。我正在将我编写的 tcl/tk gui 转换为 Python,因为我在 shell 脚本中所做的大部分事情都可以在 Python 中有效地完成,但是我在让 wigets 以这种方式排列(使用网格)时遇到了一些麻烦我想要它们。在我的示例中,请注意按钮完美排列。但是标签/条目小部件对并没有按照我想要的方式排列:注意第 3 行中的工作区/项目。我希望标签与条目小部件和中心相邻。同样在底部,我想让滚动文本加宽到它填充屏幕的宽度。我只需要几个指针,也许如何最好地将事物分组到多个框架中。此外,我假设网格行和列与根窗口相关,而不是与框架相关。
"""
PYGUITEST - This is a GUI test to test alignments of fields
Author: Jerry Feldman
"""
import sys
import os
import Tix
import tkMessageBox
import ScrolledText
import getpass
from Tkconstants import *
class pyguitest:
def __init__(self):
self.mainWindow = Tix.Tk()
self.mainWindow.tk.eval('package require Tix')
def __createUI(self, mainWindow):
mainWindow.protocol("WM_DELETE_WINDOW", self.OnExitWindow)
###### major class variables
self.username = getpass.getuser()
self.topFrame = Tix.Frame(self.mainWindow)
self.topRight = Tix.Frame(self.mainWindow)
### define buttons
self.butn01 = Tix.Button(self.topFrame,text=u"Butn01",
command=self.OnButn01ButtonClick)
self.butn02 = Tix.Button(self.topFrame,text=u"Butn02",
command=self.OnButn02ButtonClick)
self.butn03 = Tix.Button(self.topFrame,text=u"Butn03",
command=self.OnButn03ButtonClick)
self.butn04 = Tix.Button(self.topFrame,text=u"Butn04",
command=self.OnButn04ButtonClick)
self.butn05 = Tix.Button(self.topFrame,text=u"Butn05",
command=self.OnButn05ButtonClick)
self.butn06 = Tix.Button(self.topFrame,text=u"Butn06",
command=self.OnButn06ButtonClick)
self.butn07 = Tix.Button(self.topFrame,text=u"Butn07",
command=self.OnButn07ButtonClick)
self.butn08 = Tix.Button(self.topFrame,text=u"Butn08",
command=self.OnButn08ButtonClick)
self.Butn09 = Tix.Button(self.topFrame,text=u"Butn09",
command=self.OnButn09ButtonClick)
self.kwit = Tix.Button(self.topRight,text=u"quit !",
command=lambda:self.mainWindow.destroy())
### Set up the buttons grids for row 0
self.butn01.grid(column=0,row=0, sticky="nw")
self.butn02.grid(column=1,row=0, sticky="nw")
self.butn03.grid(column=2,row=0, sticky="nw")
self.butn04.grid(column=3,row=0, sticky="nw")
self.butn05.grid(column=4,row=0, sticky="nw")
self.butn06.grid(column=5,row=0, sticky="nw")
self.butn07.grid(column=6,row=0, sticky="nw")
self.butn08.grid(column=7,row=0, sticky="nw")
self.Butn09.grid(column=8,row=0, sticky="nw")
self.topFrame.grid(column=0,row=1,columnspan=8,sticky="NW")
self.kwit.grid(column=0,row=0,sticky="NE")
self.topRight.grid(column=10,row=1,columnspan=1,sticky="NE")
Tix.Label(mainWindow,text="This label should be centered").grid(row=2,sticky="nwes",columnspan=8)
wsFrame=Tix.Frame(mainWindow).grid(row=3, columnspan=9)
Tix.Label(wsFrame,text="Workspace:").grid(row=3,column=0, sticky="e")
wsEntry=Tix.Entry(wsFrame)
ws="/home/myuser/workspaces/pyguitest"
wsEntry.config(width=len(ws))
wsEntry.insert(0, ws)
wsEntry.grid(row=3, column=1)
Tix.Label(wsFrame,text="Project: ").grid(row=3,column=2, sticky="e")
self.prEntry=Tix.Entry(wsFrame)
pr=ws+"/project"
self.prEntry.grid(row=3,column=3, sticky="w")
self.prEntry.insert(0, pr)
self.prEntry.config(width=len(pr)+1)
Tix.Label(mainWindow,text="This should also be centered" ).grid(row=4,columnspan=8)
Tix.Label(mainWindow, text="ProductHome: ").grid(row=5)
ProductHome="/pmount/groupname/ProductHome/homeversion"
self.ProductHomeEntry = Tix.Entry(self.mainWindow)
self.ProductHomeEntry.delete(0, END)
self.ProductHomeEntry.insert(0, ProductHome)
self.ProductHomeEntry.configure(width=len(ProductHome)+1)
self.ProductHomeEntry.grid(row=5, column=1)
Tix.Label(mainWindow, text="Starting Port: ").grid(row=5,column=3, sticky="e")
portEntry=Tix.Entry(mainWindow)
portEntry.grid(row=5,column=4, sticky="w")
startingPort="20000"
portEntry.insert(0,startingPort)
portEntry.config(width=len(startingPort)+1)
Tix.Label(mainWindow, text="Product Version: ").grid(row=5,column=5, sticky="e")
pVersion="10.12.8 beta"
self.productVersionEntry=Tix.Entry(mainWindow)
self.productVersionEntry.grid(row=5, column=6, sticky="w")
self.productVersionEntry.insert(0, pVersion)
self.productVersionEntry.config(width=len(pVersion))
Tix.Label(mainWindow,text="Another centered label").grid(row=6, columnspan=8)
Tix.Label(mainWindow, text="Delta:").grid(row=7, column=0)
delta=ProductHome+"/delta"
self.deltaEntry=Tix.Entry(mainWindow)
self.deltaEntry.insert(0, delta)
self.deltaEntry.grid(row=7, column=1, sticky="w")
self.deltaEntry.configure(width=len(delta))
Tix.Label(mainWindow, text="Job to run: ").grid(row=7, column=2, sticky="e")
defaultJob=ProductHome+"/jobs/default.sh"
self.jobToRunEntry=Tix.Entry(mainWindow)
self.jobToRunEntry.grid(row=7, column=3, sticky="w")
self.jobToRunEntry.delete(0,END)
self.jobToRunEntry.insert(0, defaultJob)
self.jobToRunEntry.config(width=len(defaultJob))
defaultLauncher=ProductHome+"/launcher/default.sh"
Tix.Label(mainWindow, text="Launcher: ").grid(row=7, column=4, sticky="e")
self.launcherEntry=Tix.Entry(mainWindow)
self.launcherEntry.grid(row=7, column=5, sticky="w")
self.launcherEntry.delete(0,END)
self.launcherEntry.insert(0, defaultLauncher)
self.launcherEntry.config(width=len(defaultLauncher))
self.logger = ScrolledText.ScrolledText(self.mainWindow,
width = 160,
height = 60,
wrap=Tix.WORD,
relief="sunken")
self.logger.grid(row=10,columnspan=8)
def doLog(self, file):
for line in file.read().splitlines():
self.logger.insert(END, line+"\n")
txt.insert(END, "\nEnd of command\n\n")
def startpyguitest(self):
self.__createUI(self.mainWindow)
self.mainWindow.mainloop()
## Button Actions
def OnExitWindow(self):
self.mainWindow.destroy()
def OnButn01ButtonClick(self):
pass
def OnButn02ButtonClick(self):
pass
def OnButn03ButtonClick(self):
pass
def OnButn04ButtonClick(self):
pass
def OnButn05ButtonClick(self):
pass
def OnButn06ButtonClick(self):
pass
def OnButn07ButtonClick(self):
pass
def OnButn08ButtonClick(self):
pass
def OnButn09ButtonClick(self):
pass
def logIt(self, stdout):
for line in stdout.read().splitlines():
self.logger.insert(END, line+"\n")
self.logger.see(END)
if __name__ == "__main__":
pyguitest = pyguitest()
pyguitest.startpyguitest()