我使用Tkinter库在python中创建了一个GUI applcaition。我已禁用显示窗口的大小调整。我使用的所有按钮和小部件都正在我正在开发的系统上正确显示。但是当我在其他系统上运行应用程序时,小部件的大小和位置变得一团糟。
请大家建议我如何删除此问题并使应用程序系统独立?
我已附上代码文件。请在这里帮忙。
#! /usr/bin/python3.4
import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
from UserWindow import UserWindow
import swiftclient as sc
import swiftclient.exceptions as swiftClientExceptions
class MyApp(Frame):
def __init__(self,parent):
Frame.__init__(self,parent)
self.parent=parent
self.images = []
self.configureLoginWindow()
self.createLoginWindow()
def configureLoginWindow(self):
self.master.title("Music Library")
self.master.resizable(False,False)
w = 400 #Width of the Window
h = 250 #Height of the Window
# get screen width and height
ws = root.winfo_screenwidth()#This value is the width of the screen
hs = root.winfo_screenheight()#This is the height of the screen
# calculate position x, y
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
#This is responsible for setting the dimensions of the screen and where it is
#placed
self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
def createLoginWindow(self):
#Create a 5*5 Grid
self.grid()
for columns in range (0,6):
self.columnconfigure(columns, pad = 14,weight=1)
for rows in range (0,6):
self.rowconfigure(rows , pad = 14,weight=1)
#Setting the image
rawdata=Image.open('images/login.jpg')
loginImage=ImageTk.PhotoImage(rawdata)
self.images.append(loginImage)
#Creating Label to store Image
labelLoginImage = Label(image=loginImage,bg="white")
labelLoginImage.grid(column=0,row=0,columnspan=2,rowspan=5)
#Creating Login Box
labelUserName=Label(text="Username")
labelPassword=Label(text="Password")
labelWarning = Label(text="UserName OR Password is wrong!!!!",fg='red')
entryUserName=Entry(width=20)
entryPassword=Entry(width=20,show="*")
btnLogin=Button(text="Login",command=lambda:self.loginClickEvent(entryUserName.get(),entryPassword.get(),labelWarning))
btnCancel=Button(text="Cancel",command=self.cancelClickEvent)
labelUserName.grid(column=2,row=1,sticky=SW,pady=3)
labelPassword.grid(column=2,row=2,sticky=NW,pady=3)
entryUserName.grid(column=3,row=1,columnspan=2,padx=4,sticky=SW,pady=3)
entryPassword.grid(column=3,row=2,columnspan=2, padx=4,sticky=NW,pady=3)
btnLogin.grid(column=3,row=2,pady=3)
btnCancel.grid(column=4,row=2,pady=3)
labelWarning.grid_forget()
def loginClickEvent(self,usrName,password,labelWarning):
try:
#Validation with keystone client via swiftclient
storage_url , token = sc.client.get_auth("http://172.18.9.100:5000/v2.0/",usrName,password,auth_version='2.0'
,os_options=dict({"tenant_name": 'admin'}.items()))
'''storage_url , token = sc.client.get_auth("http://192.168.0.7:5000/v2.0/",usrName,password,auth_version='2.0'
,os_options=dict({"tenant_name": 'demo'}.items()))'''
storage_url = storage_url[0:7] + "172.18.9.100:8080/" + storage_url[23:]
#Passing token and storgae Uri which we have received to User Window page
app=UserWindow(storage_url , token)
except swiftClientExceptions.ClientException as unauth:
labelWarning.grid(column=2,row=3,columnspan=3,sticky='N')
print(unauth)
def cancelClickEvent(self):
self.master.destroy()
root=tk.Tk()
app = MyApp(root)
app.mainloop()
我没有足够的声誉来添加图片,请在此处提供帮助。
凌乱我意味着小部件之间的空间不均匀,大小也不同。
答案 0 :(得分:0)
我实际上已经调试了这个问题。以下是需要修改的内容: -
现在,当我将代码从Windows移动到Linux并运行时,GUI的外观看起来与众不同。第一个原因是我之前没有使用上面第1点中提到的网格方法。 其次,字体类型和大小起着至关重要的作用。如果您没有明确设置默认字体类型和大小,那么GUI会选择运行它的系统的默认字体类型。
我在Windows和Linux上手动给出了相同的字体大小和字体类型,然后按钮显示相同。
我希望答案很有帮助。在这里,我修改后粘贴代码。
import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
from UserWindow import UserWindow
import swiftclient as sc
import swiftclient.exceptions as swiftClientExceptions
import tkinter.font
class MyApp(Frame):
def __init__(self,parent):
Frame.__init__(self,parent)
self.parent=parent
self.images = []
self.configureLoginWindow()
self.createLoginWindow()
def configureLoginWindow(self):
self.master.title("Music Library")
self.master.resizable(False,False)
w = 400 #Width of the Window
h = 250 #Height of the Window
# get screen width and height
ws = root.winfo_screenwidth()#This value is the width of the screen
hs = root.winfo_screenheight()#This is the height of the screen
# calculate position x, y
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
#This is responsible for setting the dimensions of the screen and where it is
#placed
self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
def createLoginWindow(self):
#Create a 5*5 Grid
self.grid()
for columns in range (0,6):
Grid.columnconfigure(self.master,columns,weight=1)
for rows in range (0,6):
Grid.rowconfigure(self.master,rows , weight=1)
#Setting the image
rawdata=Image.open('../images/login.jpg')
loginImage=ImageTk.PhotoImage(rawdata)
self.images.append(loginImage)
#Creating Label to store Image
labelLoginImage = Label(image=loginImage,bg="white")
labelLoginImage.grid(column=0,row=0,columnspan=2,rowspan=5,sticky=NSEW)
#Creating Login Box
labelUserName=Label(text="Username" ,font=("Helvetica", 8))
labelPassword=Label(text="Password",font=("Helvetica", 8))
labelWarning = Label(text="UserName OR Password is wrong!!!!",fg='red',font=("Helvetica", 8))
entryUserName=Entry(width=20,font=("Helvetica", 8))
entryPassword=Entry(width=20,show="*",font=("Helvetica", 8))
btnLogin=Button(text="Login",padx=1,font=("Helvetica", 8),command=lambda:self.loginClickEvent(entryUserName.get(),entryPassword.get(),labelWarning))
btnCancel=Button(text="Cancel",padx=1,font=("Helvetica", 8),command=self.cancelClickEvent)
labelUserName.grid(column=2,row=1,sticky=SW,pady=3)
labelPassword.grid(column=2,row=2,sticky=NW,pady=3)
entryUserName.grid(column=3,row=1,columnspan=2,padx=4,sticky=SW,pady=3)
entryPassword.grid(column=3,row=2,columnspan=2, padx=4,sticky=NW,pady=3)
btnLogin.grid(column=3,row=2,pady=10)
btnCancel.grid(column=4,row=2,pady=10)
labelWarning.grid_forget()
def loginClickEvent(self,usrName,password,labelWarning):
try:
#Validation with keystone client via swiftclient
storage_url , token = sc.client.get_auth("http://172.18.9.100:5000/v2.0/",usrName,password,auth_version='2.0'
,os_options=dict({"tenant_name": usrName}.items()))
'''storage_url , token = sc.client.get_auth("http://192.168.0.7:5000/v2.0/",usrName,password,auth_version='2.0'
,os_options=dict({"tenant_name": 'demo'}.items()))'''
storage_url = storage_url[0:7] + "172.18.9.100:8080/" + storage_url[23:]
#Passing token and storgae Uri which we have received to User Window page
app=UserWindow(storage_url , token)
except swiftClientExceptions.ClientException as unauth:
labelWarning.grid(column=2,row=3,columnspan=3,sticky='N')
print(unauth)
def cancelClickEvent(self):
self.master.destroy()
root=tk.Tk()
app = MyApp(root)
app.mainloop()
答案 1 :(得分:0)
我认为你可以设置几何:
root.geometry("500x400+100+100")
您必须在启动Tk实例后添加它!上面代码中的500
表示宽度,此处400
表示高度,其他2个数字表示高度的位置屏幕上的窗口!!