【发布时间】: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()
再次感谢。
【问题讨论】: