我使用Tkinter库在python中创建了一个GUI应用程序。我禁用了出现的窗口的大小调整。我使用的所有按钮和小部件都在我正在开发的系统上正确显示。但是当我在其他系统上运行应用程序时,小部件的大小和位置变得混乱。如何使python GUI应用程序的桌面大小独立?
请问大家,请问我该如何解决这个问题并使应用系统独立?
我附上了代码文件。请在这里帮忙。
#! /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()
我没有足够的信誉来添加图片,请帮助这里。
通过凌乱我的意思是小部件之间的空间不均匀,大小也不同。
你使用什么布局?你是否对窗口大小进行了硬编码,或者只是禁用调整大小?显示一些代码。 –
另外,究竟是什么“变成了一团糟”呢?你可能有一些比较图像?有关这两种系统设置的更多信息也可能有用。 – fhdrsdg
我已经添加了代码和一些更多的解释。 –