2015-04-24 87 views
-1

我使用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() 

我没有足够的信誉来添加图片,请帮助这里。

通过凌乱我的意思是小部件之间的空间不均匀,大小也不同。

+1

你使用什么布局?你是否对窗口大小进行了硬编码,或者只是禁用调整大小?显示一些代码。 –

+1

另外,究竟是什么“变成了一团糟”呢?你可能有一些比较图像?有关这两种系统设置的更多信息也可能有用。 – fhdrsdg

+0

我已经添加了代码和一些更多的解释。 –

回答

0

我实际上已经调试过这个问题。以下是需要修改的内容: -

  1. 在网格中配置行和列时。使用Grid.columnconfigure和Grid.row配置,权重为1或更大,然后为1.
  2. 窗口小部件的width属性可能因使用的窗口小部件的类型而异。在具有文本值的小部件的情况下,宽度以字符数指定,如果小部件具有图像,则宽度以像素指定。

现在,当我已经将我的代码从Windows移到Linux并运行时,那么GUI看起来不同。第一个原因是我之前没有使用上面第1点提到的Grid方法。其次,字体的类型和大小起着至关重要的作用。如果您不明确设置默认的字体类型和大小,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() 
0

我想你可以设置几何图形: root.geometry("500x400+100+100")

您必须启动Tk的实例后加入吧!上面的代码中的500表示宽度,而400这里表示高度,其他两个数字表示窗口在屏幕上的位置!

相关问题