Tkinter是“Tk”图形用户界面工具包的标准Python接口。在Python 3中,模块的名称从Tkinter更改为tkinter。
import os, signal if __name__ == "__main__": app = Moisture() app.mainloop() try: app.P.kill() except: pass try: app.L.kill() except: pass PID = os.getpid() os.kill(PID, signal.SIGKILL)
TKINTER提供了一个用于打开一个文件(AskOpenFilename)和几个文件(AskOpenFileNames)的对话框,但缺少几个目录的对话框。
Python&tkinter:如何为图像分配/更改特定位置?
,这是我代码的相关摘录:
如何添加一个用于CTK文本框的分离器? 我想在文本框的条目之间添加一个分隔线,但尚未找到一种整洁的方法。 我也很乐意感谢我对我的代码的任何进一步评论,任何方法t ...
import customtkinter as cTk class MainWindow(cTk.CTk): def __init__(self): super().__init__() self.title("Separator Text Box Test") self.geometry("1280x600") self.text_box = cTk.CTkTextbox(self) self.text_box.place(relx=0.05, rely=0.07, relwidth=0.9, relheight=0.45) self.text_box.configure(state="disabled") # TESTING self.TestingFrame = cTk.CTkFrame(self) self.TestingFrame.place(relx=0.05, rely=0.55, relwidth=0.9, relheight=0.1) self.TestingEntry = cTk.CTkEntry(self.TestingFrame) self.TestingEntry.pack(side="left", fill="x", expand=True, padx=5, pady=5) self.TestingButton = cTk.CTkButton(self.TestingFrame, text="Add Text Field", command=self.add_text_to_textbox) self.TestingButton.pack(side="right", padx=5, pady=5) self.TestingDeleteButton = cTk.CTkButton(self, text="Delete All Entries", command=self.delete_all_entries) self.TestingDeleteButton.place(relx=0.05, rely=0.7, relwidth=0.4, relheight=0.05) self.TestingPrintButton = cTk.CTkButton(self, text="Print Latest Entry", command=self.print_latest_entry_from_textbox) self.TestingPrintButton.place(relx=0.55, rely=0.7, relwidth=0.4, relheight=0.05) def add_text_to_textbox(self): # add separator using autoseparators maybe - dont actually know what that does text = self.TestingEntry.get() + "\n" if text != "": self.text_box.configure(state="normal") self.text_box.insert("0.0", text) self.text_box.configure(state="disabled") def delete_all_entries(self): self.text_box.configure(state="normal") self.text_box.delete("0.0", "end") self.text_box.configure(state="disabled") def print_latest_entry_from_textbox(self): text = self.text_box.get("1.0", "end-1c").split('\n')[0].strip() if text != "": print(f"Latest Entry: {text}") else: print("No entries available!") if __name__ == "__main__": root = MainWindow() root.mainloop()
ttk.Panedwindow
import tkinter as tk from tkinter import * import ttkbootstrap as tb #root = tk.Tk() root=tb.Window(themename="terry") root.title("Stableford League") root.geometry("1000x1000") root.mainloop()
以防止TKINTER在选定帧中绘制/绘制的每个绘制图上打开图形?
#My Imports import numpy as np import pandas as pd import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.dates as mdates from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk) from matplotlib.backend_bases import key_press_handler import tkinter as tk import tkinter.ttk as ttk from tkinter import filedialog from datetime import datetime from PIL import ImageTk, Image import seaborn as sns from scipy.stats import linregress #Setting the TKinter window settings window = tk.Tk() screen_width = window.winfo_screenwidth() screen_height = window.winfo_screenheight() window.title("Data Visualizer - Plotting in TKinter") window.geometry(str(int(screen_width*.8))+"x"+str(int(screen_height*.8))) #A function so that selecting the graph doesn't auto plot def nullFunction(*args): print("nullFunction called") #Create test pd.DataFrame #Just something simple to visualize; more columns in actual data df = pd.DataFrame( {"time" : [0,1,2,3,4,5,6,7,8,9], "linear" : [0,1,2,3,4,5,6,7,8,9], "random" : [0,8,4,6,4,7,1,9,2,6], "inverse" : [9,8,7,6,5,4,3,2,1,0]} ) #List the various data columns to be selected later, don't list 'time' #Called on button press after df is created/uploaded def generate_List(): i = 0 listbox.delete('0','end') for column in df.columns: if column == 'time': pass else: listbox.insert(i,column) if (i%2) == 0: listbox.itemconfigure(i, background = '#f0f0f0') i = i+1 plt.ion() #make figure as its own element class FigureCreate: def __init__(self, figsize=(5, 5)): self.fig = plt.figure(figsize=figsize) def get_figure(self): return self.fig #make axes based on listbox selection class AxesCreate: def __init__(self, figure, position=(1, 1, 1)): self.ax = figure.add_subplot(*position) self.figure = figure def get_axes(self): return self.ax def get_fig(self): return self.figure #plot a preselected group of elements; not currently used class CanvasDraw: def __init__(self, axes): self.ax = axes def plot(self, x, y, label=''): self.ax.plot(x, y, label=label) def show(self): self.ax.legend() plt.show() #Called on button press after list is created def plotSet(*args): plt.close #Possibly redundant figure and axis creation methods fig_create = FigureCreate(figsize=(5,5)) fig = fig_create.get_figure() ax_create = AxesCreate(fig, position=(1,1,1)) ax = ax_create.get_axes() #canvas_draw = CanvasDraw(ax) selection = listbox.curselection() for trace in selection: sns.lineplot(data=df, x='time', y=df.columns[trace],ax=ax) #Vlines that will be adjustable after plot issues are solved plt.axvline(df['time'][2],color='green',linestyle='--') plt.axvline(df['time'][4],color='red',linestyle='--') return (ax_create.get_fig()) def plot_MC(fig): # Creating the Tkinter canvas containing the figure #THIS IS WHERE I THINK THE PROBLEM IS, BUT I'M NOT SURE #I thought this would make the canvas in the MC frame, but it seems to want a new plot since plotSet() is returning a figure #Is there a better way to plot specifically in the middle frame, and not force plotSet() to return anything? canvas = FigureCanvasTkAgg(fig, master=frame_MC) canvas.draw() # Placing the canvas on the Tkinter window canvas.get_tk_widget().grid(row=0, column=0) # Creating the Matplotlib toolbar toolbar = NavigationToolbar2Tk(canvas, frame_MC, pack_toolbar=False) toolbar.update() toolbar.grid(row=1, column=0) def are_you_sure(): """ Just a function that opens a message box to confirm window destruction """ if tk.messagebox.askyesno("Quit Dialog","Are you sure you want to quit the app?"): window.destroy() #MC 'Middle-Center' frame_MC = tk.Frame(window) #ML 'Middle-Left' list_frame=tk.Frame(window) listbox=tk.Listbox(list_frame, selectmode='multiple', height=30, width=30) listbox.bind('<<ListboxSelect>>', plotSet) scrollbar=tk.Scrollbar(list_frame, orient="vertical") listbox.config(yscrollcommand=scrollbar.set) scrollbar.config(command = listbox.yview) #TL 'Top-Left' frame_TL = tk.Frame(window) generate_list_button = tk.Button(frame_TL, text="generate list", command=generate_List, width=30) generate_list_button.grid(row=0, column=0, columnspan=3) plot_test_button = tk.Button(frame_TL, text="plot selection", command=plot_MC(plotSet())) plot_test_button.grid(row=1, column=0, columnspan=3) #BL 'Bottom-Left' frame_BL = tk.Frame(window) button_quit = tk.Button(master = frame_BL, text = "Quit", command = are_you_sure, bg = '#FF2222') button_quit.grid(row = 0, column = 0) #Placing elements frame_TL.grid(row=0, column = 0) frame_MC.grid(row = 1, column = 1) frame_BL.grid(row = 2, column = 0) list_frame.grid(row = 1, column = 0) listbox.pack(side = "left", fill = "y") scrollbar.pack(side = "right", fill = "y") #run the gui window.mainloop()
韦尔,最好将Kivy框架用于Python上的移动开发。但是,如果您真的想使用移动设备上的TKINTER运行您的程序,则可以尝试使用Pydroid3应用程序,可以在游戏市场中获取它。只需在手机上复制项目的源代码,用pydroid打开并运行它(首先,您需要在此应用中安装TKINTER)
我正在尝试使用tkinter在VSCODE中进行GUI应用程序,但是当我运行程序时,我没有窗口。 我的代码如下:
在命令行中仅使用Python运行tkinter,但是当我尝试使用VSCODE时。任何帮助都将不胜感激!
如何从启动(Raspberry PI4B+)tkinter Python程序运行 - 错误没有显示名称
都可以使用,但是每次启动时,我都有错误“ _tkinter.tclerror:无显示名称,也没有$显示环境变量”。 我检查了解决方案,但每个人都在谈论我不使用的“ matplotlib” 我的启动器是我的启动器: