相关疑难解决方法(0)

在tkinter中切换两帧

我已经构建了我的前几个脚本,它们上面有一个很好的小GUI,正如教程向我展示的那样,但它们都没有说明如何处理更复杂的程序.

如果你有一个带有"开始菜单"的东西,对于你的开始屏幕,并且在用户选择后你移动到程序的不同部分并适当地重新绘制屏幕,​​这样做的优雅方式是什么?

一个只是.destroy()'开始菜单'框架,然后创建一个新的填充小部件的另一个部分?当他们按下后退按钮时反转这个过程?

python tkinter frame python-3.x

78
推荐指数
3
解决办法
11万
查看次数

多个窗口的Tkinter示例代码,为什么按钮不能正确加载?

我正在写一个程序应该:

  1. 只需按一下按钮即可打开一个窗口.
  2. 按下另一个按钮关闭新打开的窗口.

我正在使用类,所以我可以稍后将代码插入到更大的程序中.但是,我无法正确加载我的按钮.

import tkinter as tk

class Demo1(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        self.pack()
        self.master.title("Demo 1")
        self.button1 = tk.Button(self, text = "Button 1", width = 25,
                               command = self.new_window)
        self.button1.grid(row = 0, column = 1, columnspan = 2, sticky = tk.W+tk.E+tk.N+tk.S)

    def new_window(self):
        self.newWindow = Demo2()

class Demo2(tk.Frame):
    def __init__(self):
        new = tk.Frame.__init__(self)
        new = tk.Toplevel(self)
        new.title("Demo 2")
        new.button = tk.Button(text = "Button 2", width = 25,
                               command = self.close_window)
        new.button.pack()

    def close_window(self):
        self.destroy()

def main():
    Demo1().mainloop()

if __name__ == …
Run Code Online (Sandbox Code Playgroud)

python class tkinter destroy

31
推荐指数
2
解决办法
10万
查看次数

在Tkinter应用程序中是否继承Frame

我已经看到了两种设置tkinter程序的基本方法.有没有理由更喜欢一个到另一个?

from Tkinter import *

class Application():
    def __init__(self, root, title):
        self.root = root
        self.root.title(title) 

        self.label = Label(self.root, text='Hello')
        self.label.grid(row=0, column=0)  

root = Tk()
app = Application(root, 'Sample App')
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

from Tkinter import *

class Application(Frame):
    def __init__(self, title, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.master.title(title) 

        self.label = Label(self, text='Hello')
        self.label.grid(row=0, column=0) 

app = Application('Sample App')
app.mainloop()   
Run Code Online (Sandbox Code Playgroud)

python tkinter

28
推荐指数
2
解决办法
3万
查看次数

如何构建复杂的面向对象的 tkinter 程序

我已经使用 python 编写了多个 gui 应用程序,但每个应用程序都是一堆初始化和函数,我不觉得我正在充分利用面向对象的功能。我在网上搜索过,但没有找到任何显示此类程序最佳实践的地方。那么,对于具有多个标签、菜单、按钮和屏幕以及一些后端计算的复杂 GUI 应用程序,构建程序的最佳方法是什么?

目前,我只是在类的构造函数中对框架、标签等进行所有初始化,并调用一个函数来循环并执行后端操作。

user-interface tkinter python-3.x

6
推荐指数
1
解决办法
1941
查看次数

用鼠标选择图像区域并记录选择的尺寸

我希望能够使用鼠标光标在我的程序中显示的图像上拖动矩形选择并读取选择的尺寸,以便我以后可以使用它们来裁剪该图像。我如何在 Python 3 中做到这一点?

更新:

假设我这样做:

import tkinter as tk
from PIL import ImageTk, Image

#This creates the main window of an application
window = tk.Tk()
window.title("Join")
window.geometry("900x900")
window.configure(background='grey')

path = "Book.jpg"

#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(Image.open(path))

#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)

#The Pack geometry manager packs widgets …
Run Code Online (Sandbox Code Playgroud)

python tkinter python-imaging-library python-3.x

6
推荐指数
2
解决办法
4579
查看次数

如何将 Python Tkinter 代码拆分为多个文件

这是我在此的头一篇博文!

\n\n

首先这是我的项目的 github 链接(我也是 github 上的菜鸟)。

\n\n

编辑:

\n\n

这是我想要做的一个例子,我有一个大的 Tkinter 类,其中有框架、标签、菜单、按钮以及所有和一些功能。

\n\n

我想让 UI 描述在我的 MakeUI() 中并将我的函数移动到另一个文件,但我仍然需要访问小部件。

\n\n

<主.py>

\n\n
# -*- coding: utf-8 -*-\n\nfrom tkinter import *\nfrom Interface import *\n\n\nFenetre = Tk()\nUI = MakeUI(Fenetre)\n\nUI.mainloop()\n\nUI.destroy()\n
Run Code Online (Sandbox Code Playgroud)\n\n

<接口.py>

\n\n
# -*- coding: utf-8 -*-\n\nfrom tkinter import *\nfrom tkinter.filedialog import *\n\n\nclass MakeUI(Frame):\n\n    def __init__(self, Fenetre, **kwargs):\n\n        # H\xc3\xa9ritage\n        Frame.__init__(self, Fenetre, width=1500, height=700, **kwargs)\n\n        self.pack(fill=BOTH)\n\n        self.FrameInfos = LabelFrame(self, text="Choix des param\xc3\xa8tres", padx=2, pady=2)\n        self.FrameInfos.pack(fill="both", expand="yes", padx=5, pady=5)\n\n        self.MsgInfosCarte = Label(self.FrameInfos, …
Run Code Online (Sandbox Code Playgroud)

python tkinter python-3.x

5
推荐指数
1
解决办法
5061
查看次数

TypeError:元类冲突:派生类的元类必须是其所有基类的元类的(非严格)子类

尝试使用类创建 GUI,但我一直遇到此错误的问题。我不确定这意味着什么,因为据我所知我只有一门课,我的错误是:

Traceback (most recent call last):
File "C:/Users/Blaine/Desktop/Computing Project.py", line 5, in <module>
class SneakerSeeker(tk,Frame):
TypeError: metaclass conflict: the metaclass of a derived class must be a 
(non-strict) subclass of the metaclasses of all its bases
Run Code Online (Sandbox Code Playgroud)

我的代码是:

from tkinter import * 
import tkinter as tk
import tkinter.messagebox as tm

class Number1(tk,Frame):
    def __init__(self, master):
        super(Number1, self).__init__()
        self.master = master
        self.frame = tk.Frame(self.master)
        self.TopTitle = Label("Number1", font = ('Calibri ', 16))
        self.TopTitle.pack()


def main():
    root = tk.Tk()
    root.title("Number 1")
    app …
Run Code Online (Sandbox Code Playgroud)

python user-interface class tkinter python-3.x

5
推荐指数
1
解决办法
9492
查看次数

在 Python 中使用滚动条显示大图像

我想使用 tkinter (Python 3) 显示图像。图片很长。因此,我想添加一个垂直滚动条。这是我尝试过的:

(基于这个问题:Scrollbars for a .jpg image on a Tkinter Canvas in Python

import tkinter
import PIL.Image, PIL.ImageTk

# Create a window
window = tkinter.Tk()

frame = tkinter.Frame(window, bd=2) # relief=SUNKEN)

frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)

xscrollbar = tkinter.Scrollbar(frame, orient=tkinter.HORIZONTAL)
xscrollbar.grid(row=1, column=0, sticky=tkinter.E+tkinter.W)

yscrollbar = tkinter.Scrollbar(frame)
yscrollbar.grid(row=0, column=1, sticky=tkinter.N+tkinter.S)

canvas = tkinter.Canvas(frame, bd=0, xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
canvas.grid(row=0, column=0, sticky=tkinter.N+tkinter.S+tkinter.E+tkinter.W)
canvas.config(scrollregion=canvas.bbox(tkinter.ALL))

File = "FILEPATH"
img = PIL.ImageTk.PhotoImage(PIL.Image.open(File))
canvas.create_image(0,0,image=img, anchor="nw")

xscrollbar.config(command=canvas.xview)
yscrollbar.config(command=canvas.yview)

frame.pack()
window.mainloop()
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

我能够绘制图像,但滚动条不起作用。它们只是灰色且不起作用。

在此处输入图片说明

python tkinter

3
推荐指数
1
解决办法
4581
查看次数

Python Tkinter中的单选按钮值

我正在尝试使用Tkinter在Python中创建一个对话框.目标是有一个带有两个单选按钮和一个"确定"按钮的对话框.单选按钮1选择"默认"选项.单选按钮2选择"用户定义"选项."确定"按钮关闭窗口.

问题1:如何从单选按钮保存值?也就是说,如何将选定的单选按钮传递给我的其余脚本?

问题2: 如何让第二个单选按钮包含用户文本输入(沿着tkSimpleDialog.askstring的行)?我希望该按钮显示一个单选按钮,一个提示符("输入值:"),以及一个供用户输入文本的空间 - 所有这些都在一行作为单个单选按钮选项.
因此整个对话框应该将顶部单选按钮设置为普通单选按钮,第二个按钮指定用户输入并包含该用户输入的空间(以及"确定"按钮).

到目前为止,我打开了一个带有两个选项的对话框,但是这个值没有传递给我能看到的任何东西; 在我选择单选按钮之前,选择返回为0.
任何问题的任何帮助将不胜感激,谢谢.
到目前为止这是我的脚本:

from Tkinter import*

master = Tk()
var = IntVar()
Label(master, text = "Select OCR language").grid(row=0, sticky=W)
Radiobutton(master, text = "default", variable = var, value = 1).grid(row=1, sticky=W)
Radiobutton(master, text = "user-defined", variable = var, value = 2).grid(row=2, sticky=W)
Button(master, text = "OK", command = master.quit).grid(row=3, sticky=W)
selection = var.get()
print "Selection:", selection
mainloop()
#If selection == 0 do one thing
#If selection == 1 do something else...
Run Code Online (Sandbox Code Playgroud)

python tkinter radio-button

2
推荐指数
1
解决办法
2万
查看次数

Python 3和Tkinter。全局变量太多

我开始使用Tkinter库在Python中编写一个简单的GUI应用程序。一切正常,但是要动态更改任何小部件(例如,将按钮从“登录”更改为“注销”等),我必须从外部获取包含小部件对象本身的变量。我可以通过两种方式做到这一点:

  1. 将小部件作为参数传递;
  2. 使用全局变量。

一个简化的例子:

1)作为参数传递:

def changeLabel(b):
    b.config(text='Logout')

btn1 = Button(f0,text="Login",width=15)
btn1.grid(row=1,column=2,rowspan=1,padx=10,ipadx=10)
changeLabel(btn1)
Run Code Online (Sandbox Code Playgroud)

2)或使用全局变量:

def changeLabel():
    global btn1
    btn1.config(text='Logout')

btn1 = Button(f0,text="Login",width=15)
btn1.grid(row=1,column=2,rowspan=1,padx=10,ipadx=10)
changeLabel(btn1)
Run Code Online (Sandbox Code Playgroud)

现在,我知道应该避免使用全局变量,但是将每个小部件作为参数通过许多函数传递,即使在简单的应用程序中也很麻烦。

那么,在运行时操作Tkinter小部件的最佳方法是什么?你能建议我正确的方法吗?谢谢

python tkinter python-3.x

1
推荐指数
1
解决办法
254
查看次数