【问题标题】:Use button function to open file browser and display image files使用按钮功能打开文件浏览器并显示图像文件
【发布时间】:2019-09-27 18:57:32
【问题描述】:

我对 Python 完全陌生,在弄清楚如何从文件浏览器打开和显示图像文件时遇到了一些麻烦。我尝试使用 appJar 和 Tkinter。 从 Tkinter,我可以选择图像文件,但不会显示 iamge。 从 appJar,我只是不知道如何使用按钮打开文件浏览器。 在这里我附上了代码,我想知道是否有人能够帮助我。谢谢。

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter.messagebox import showinfo
from tkinter.filedialog import askopenfile 



def popup_bonus():
    win = tk.Toplevel()
    win.wm_title("Window")


    a = ttk.Button(win, text="Open")
    a.grid(row=0, column=0)


    b = ttk.Button(win, text="Exit", command=win.destroy)
    b.grid(row=1, column=0)

def popup_showinfo():
    showinfo("About", "My Name, Python Version")

class Application(ttk.Frame):

    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.pack()

        self.button_bonus = ttk.Button(self, text="File", command=popup_bonus)
        self.button_bonus.pack()

        self.button_showinfo = ttk.Button(self, text="Help", command=popup_showinfo)
        self.button_showinfo.pack()

root = tk.Tk()

app = Application(root)

root.mainloop()

这是使用 appJar 的代码

#import the library
from appJar import gui
import os

#create a GUI variable called app
app = gui('Window', '400x200')
app.setBg('silver')


#habdle button events
def press(button):
    if button == 'About':
        app.infoBox(title = 'About', message = 'Python Verison, Author')

    if button == 'Exit':
        app.stop()

    if button == 'Open':
        app.openBox(title=None, dirName=None, fileTypes=[('images', '*.png'), ('images', '*.jpg')], asFile=True, parent=None, multiple=False, mode='w')

# add & configure widgets - widgets get a name,to help referencing
app.setFont(size=18, family='Calibri', underline=False)
app.addLabel('title', 'File Select')
app.setLabelBg('title', 'grey')
app.setLabelFg('title','black')


# link the buttons to the function called press
fileMenu = ['Open','-','Exit']
helpMenu = ['About']

app.addMenuList('File', fileMenu, press)
app.addMenuList('Help', helpMenu, press)


# start the GUI
app.go()

再次感谢。

【问题讨论】:

    标签: button tkinter appjar


    【解决方案1】:

    在 appJar 中,您需要在 GUI 中添加一个按钮,该按钮在按下时运行您定义的功能: app.addButton('Open', press)

    这将导致显示openBox

    然后,在您的函数中,您需要捕获openBox() 函数的返回值(图像路径),以便您可以显示它:

    filename = app.openBox('Select Image')
    app.addImage('img', filename)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      相关资源
      最近更新 更多