Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tkinter 如何使python GUI应用程序与桌面大小无关?_Tkinter_Python 3.4 - Fatal编程技术网

Tkinter 如何使python GUI应用程序与桌面大小无关?

Tkinter 如何使python GUI应用程序与桌面大小无关?,tkinter,python-3.4,Tkinter,Python 3.4,我使用Tkinter库在python中创建了一个GUI应用程序。我已禁用调整显示窗口的大小。我使用的所有按钮和小部件都正确地显示在我正在开发的系统上。但当我在其他系统上运行应用程序时,小部件的大小和位置变得一团糟 你们能建议我如何消除这个问题并使应用程序系统独立吗 我已经附上了代码文件。请帮忙 #! /usr/bin/python3.4 import tkinter as tk from tkinter import * from PIL import ImageTk, Image from

我使用Tkinter库在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()
我没有足够的声誉来添加图片,请帮助这里

#! /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()

我所说的凌乱意味着小部件之间的空间不均匀,大小也不同。

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

  • 在网格中配置行和列时。使用Grid.columnconfigure和Grid.rowconfigure,权重为1或大于1
  • widget的width属性可以随使用的widget的种类而变化。如果小部件具有文本值,则宽度以字符数指定,如果小部件具有图像,则宽度以像素指定 现在,当我将代码从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()
    

    我认为您可以设置几何图形:
    root.geometry(“500x400+100+100”)


    您必须在启动Tk实例后添加它!上述代码中的
    500
    表示宽度,此处的
    400
    表示高度,其他两个数字表示窗口在屏幕上的位置

    你用什么布局?您是硬编码窗口大小,还是只是禁用了调整大小?显示一些代码。另外,
    变得一团糟到底是什么意思?你可能有一些比较图片吗?关于这两个系统设置的更多信息也可能有用。我已经添加了代码和一些解释。