我在 Tkinter 中嵌入 MatPlotLib 图形时遇到了问题,在 Google 和 MatPlotLib 网站上进行了一些搜索后,我能得到的最好的方法是标准方法:
import tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
fig = Figure(figsize=(5, 4), dpi=100)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root) toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试用 .grid 替换打包布局(并删除 .pack() 参数),我会收到一堆错误,并且无论我尝试了多少次 Google 搜索,嵌入 MatPlotLib 的所有方法Tkinter 中的图仅使用 pack 方法。有人可以帮我解决这个问题吗?我想嵌入图形,但使用网格方法,因为我的 GUI 应用程序的其余布局是 .grid 布局。
我在 Tkinter 中的导航工具栏遇到的另一个问题是导航工具栏显然可以自定义(至少根据SentDex [5:18])。他似乎没有详细说明我如何做到这一点,这对我来说很困难,因为我对 MatPlotLib 的按钮不太满意(它们看起来非常古老和过时)。
有人可以帮我解决这个问题吗?当我只放入图表时,它似乎工作得很好,但是当我尝试将图表放入导航工具栏时,我也会遇到问题。对此的任何帮助将不胜感激。谢谢!
我正在尝试将使用 openpyxl 创建的 Excel 工作簿保存到用户通过 Tkinter“浏览”按钮输入的特定目录。我将工作簿保存在输入的“保存点”处,但收到一条错误消息,指出它是一个目录。
在生成工作簿的函数中,我有:
wb.save(save_spot)
Run Code Online (Sandbox Code Playgroud)
“保存点”是通过函数生成的:
def set_save_destination():
global save_spot
save_spot = filedialog.askdirectory()
save_spot = str(save_spot)
Run Code Online (Sandbox Code Playgroud)
用户可以在我的 GUI 类中通过以下 Tkinter GUI 代码选择目录:
monthly_browse = ttk.Button(self, text='Select Save Destination', command=set_save_destination)
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息是“IsADirectoryError”,但我不确定问题是什么,因为它说您可以直接将目录输入到保存方法中。我是编程新手,完全自学,所以任何帮助都会很棒!谢谢你!
我尝试遵循一些教程来了解如何让画布滚动,但它最终总是呈灰色。我尝试在画布上添加一个滚动区域,但我不理解它,只知道它没有变灰,但仍然什么也没做。
我的代码(运行后将其调整为垂直较短,但它仍然呈灰色):
self.main_window = Tk()
self.root = Frame(self.main_window, bg="white")
self.root.pack(fill=BOTH, expand=1)
# TOP SECTION
self.top = Frame(self.root, bg="white")
self.top.pack(fill=BOTH, expand=1)
Label(self.top, text="#########################################").pack()
# BOTTOM SECTION
self.bottom = Frame(self.root, bg="white")
self.bottom.pack(fill=BOTH, expand=1)
# BOTTOM-LEFT SECTION
self.canvas = Canvas(self.bottom, bg="white")
self.canvas.pack(fill=BOTH, expand=1, side=LEFT)
self.left = Frame(self.canvas)
self.left.pack(fill=BOTH, expand=1)
left_scroll = Scrollbar(self.left, orient=VERTICAL)
left_scroll.pack(side=RIGHT, fill=Y)
left_scroll.config(command=self.canvas.yview)
self.canvas.configure(yscrollcommand=left_scroll.set)
for root, dirs, files in os.walk("C:\\", topdown=True):
full = dirs + files
for i in full:
Button(self.left, text=i, bg="white", anchor="w", relief=SOLID, borderwidth=0).pack(fill=BOTH)
break
# BOTTOM-RIGHT SECTION …Run Code Online (Sandbox Code Playgroud) 我想将“Enter”键仅绑定到其中一个选项卡,这是示例代码
from tkinter import *
from tkinter import ttk
def hello(event):
print('hello')
window = Tk()
window.geometry('500x500')
tab_control = ttk.Notebook(window)
tab_1 = ttk.Frame(tab_control)
tab_2 = ttk.Frame(tab_control)
tab_control.add(tab_1, text='Tab 1')
tab_control.add(tab_2, text='Tab 2')
tab_control.pack(expand=1, fill='both')
window.bind('<Return>', hello)
window.mainloop()
Run Code Online (Sandbox Code Playgroud)
window.bind()将允许用户在两个选项卡中运行hello()功能。但我只想将它绑定到tab_1.
我尝试过tab_1.bind('<Return>', hello),但两个选项卡中都没有任何响应。
我从文本文件中读取了复选框的输入,并且该输入的增加超过了给定的窗口大小。如何为此添加滚动条以查看复选框的所有内容。提前致谢。
编辑:添加了代码。
from tkinter import *
import os
error = []
window = ""
with open("op.txt") as inp:
for lines in inp:
if lines.strip() == "done":
error.append(window)
window = ""
else:
window += lines
print(len(error))
root = Tk()
root.minsize(500, 500)
cbTexts={}
cbVariables={}
cb={}
for i in error:
cbTexts[i] = StringVar()
cbTexts[i].set(i)
cbVariables[i] = IntVar()
cbVariables[i].set(0)
cb[i] = Checkbutton(root, textvariable=cbTexts[i], variable=cbVariables[i])
cb[i].pack()
mainloop()
Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法获取画布中包含标签的所有项目。据我所知,该功能canvas.find_withtag(tag)只允许一个标签,我希望能够做到:canvas.find_withtag(tag0, tag1, ...)。基本上有一种干净的方法可以获取与多个标签匹配的物品,而无需使用套装。有没有一种干净的方法或者我必须使用 canvas.find_withtag(tag) 自己完成逻辑?
我实际上问的是是否有更好的方法来实现这一点:
itemstag1 = set(self.v_maps.canvas.find_withtag(tag1))
itemstag2 = set(self.v_maps.canvas.find_withtag(tag2))
for item in itemstag1 &itemstag2:
self.canvas.itemconfig(item, fill=color)
Run Code Online (Sandbox Code Playgroud) 我是 Python 新手,我最近 1 周前开始学习,但我被困在这里......任何帮助将不胜感激......
from tkinter import *
import tkinter as tk
import psycopg2
root = Tk()
def get_info(Employee_Name, Department, Education,Salary):
con = psycopg2.connect(dbname = 'postgres',user = 'postgres',password = 'Gaurav@31',host = 'localhost',port = 5432)
cur = con.cursor()
query = ('''INSERT INTO Employee (Employee_name, Department, Education,Salary) VALUES (%s,%s,%s,%s);''' )
cur.execute(query,(Employee_Name, Department, Education,Salary))
print('Data inserted Successfully')
con.commit()
con.close()
display_all()
def search(emp_id):
con = psycopg2.connect(dbname = 'postgres',user = 'postgres',password = 'Gaurav@31',host = 'localhost',port = 5432)
cur = con.cursor()
query = ('''SELECT * …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Tkinter GUI 中创建一个天气应用程序。它运行良好。我想为其添加背景图像,但它不会显示。最初我在没有参考线的情况下做到了这一点,但它不起作用。在一些网站上,他们说要保留图像的参考,但这也不起作用。另外,我的 Tkinter 窗口大小是 1920x1080,图像尺寸相同,但仍然不显示。我尝试减小图像大小而不是窗口大小,但仍然不起作用。任何建议都被接受。也没有错误。
bg = PhotoImage('clearsky2.jpg')
bgl = Label(gui,image=bg)
bgl.image = bg #given a reference
bgl.place(x=0, y=0, relwidth=1,relheight=1)
bgl.pack()
Run Code Online (Sandbox Code Playgroud) 我正忙于使用 TKinter GUI,并尝试将其他项目附加到滚动条画布中。滚动条位似乎工作得很好
def canvas_configure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
canvas.bind("<Configure>", lambda event: self.canvas_configure(event))
Run Code Online (Sandbox Code Playgroud)
配置函数在调整大小时触发得很好,但在附加项目时不会触发,我想知道是否可以手动触发<Configure>(或与此相关的其他事件)
def append_to_canvas(self, parent):
label = tk.Label(parent, text="Yes")
label.pack();
# canvas execute <Configure>
Run Code Online (Sandbox Code Playgroud)
笔记
我目前处理此类情况的方法是仅执行事件将执行的相同函数,例如
def append_to_canvas(self, parent):
label = tk.Label(parent, text="Yes")
label.pack()
event = # create imposter event here
self.canvas_configure(event)
Run Code Online (Sandbox Code Playgroud)
但这并不总是实用
明晰
我上面使用的例子只是一个例子。我更关心问题的手动触发绑定事件部分。
这适用于其他绑定事件,例如
button.bind("<Button-1>", lambda event: exec_button_click());
Run Code Online (Sandbox Code Playgroud) 我正在使用 Tkinter 创建一个计算器应用程序。我需要多种类型的异常语句,通过针对用户引起的不同异常的不同类型的输出来告知用户。
例如:
如果用户将数字除以零,则会出现除零错误。在这种情况下,我必须创建一个仅处理除零错误的异常语句。
同时,如果用户输入“100++2”而不是“100+2”等无效操作,则会出现无效语法错误。在这种情况下,我必须在同一个尝试中指定不同的异常语句,以不同的方式处理这种情况。
那么,如何做到这一点呢?
python ×10
tkinter ×10
python-3.x ×2
canvas ×1
events ×1
exception ×1
graph ×1
matplotlib ×1
openpyxl ×1
photoimage ×1
scrollbar ×1