添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Python Tkinter 画30个滑块和一个按钮及其他

Python Tkinter 画30个滑块和一个按钮及其他

2 年前 · 来自专栏 无痛+英语

Python Tkinter 画30个滑块和一个按钮及其他






## from tkinter import *
import tkinter as tk
def show_values():
    print (wSH1.get())
    print (sV_1.get(), sV_2.get(),sV_3.get(),sV_4.get(),sV_5.get(),sV_6.get(),sV_7.get(),
    sV_8.get(),sV_9.get(),sV_10.get(),sV_11.get(),sV_12.get(),sV_13.get(),sV_14.get(),sV_15.get(),
    sV_16.get(),sV_17.get(),sV_18.get(),sV_19.get(),sV_20.get(),sV_21.get(),sV_22.get(),sV_23.get(),
    sV_24.get(),sV_25.get(),sV_26.get(),sV_27.get(),sV_28.get(),sV_29.get() )
master = tk.Tk()
## tk.Button (master, text='Show', command=show_values).pack(side= tk.LEFT and 'bottom' )
tk.Button (master, text='Show', command=show_values).pack( )
wSH1 = tk.Scale(master, from_=0, to=100, length=1200,tickinterval=10, orient=tk.HORIZONTAL)
wSH1.set(32)
# wSH1.pack(side='top' )
wSH1.pack()
sV_1 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_1.set(80)
sV_1.pack(side='left' )
sV_2 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_2.set(80)
sV_2.pack(side='left' )
sV_3 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_3.set(80)
sV_3.pack(side='left' )
sV_4 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_4.set(80)
sV_4.pack(side='left' )
sV_5 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_5.set(80)
sV_5.pack(side='left' )
sV_6 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_6.set(80)
sV_6.pack(side='left' )
sV_7 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_7.set(80)
sV_7.pack(side='left' )
sV_8 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_8.set(80)
sV_8.pack(side='left' )
sV_9 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_9.set(80)
sV_9.pack(side='left' )
sV_10 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_10.set(80)
sV_10.pack(side='left' )
sV_11 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_11.set(80)
sV_11.pack(side='left' )
sV_12 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_12.set(80)
sV_12.pack(side='left' )
sV_13 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_13.set(80)
sV_13.pack(side='left' )
sV_14 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_14.set(80)
sV_14.pack(side='left' )
sV_15 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_15.set(80)
sV_15.pack(side='left' )
sV_16 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_16.set(80)
sV_16.pack(side='left' )
sV_17 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_17.set(80)
sV_17.pack(side='left' )
sV_18 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_18.set(80)
sV_18.pack(side='left' )
sV_19 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_19.set(80)
sV_19.pack(side='left' )
sV_20 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_20.set(80)
sV_20.pack(side='left' )
sV_21 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_21.set(80)
sV_21.pack(side='left' )
sV_22 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_22.set(80)
sV_22.pack(side='left' )
sV_23 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_23.set(80)
sV_23.pack(side='left' )
sV_24 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_24.set(80)
sV_24.pack(side='left' )
sV_25 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_25.set(80)
sV_25.pack(side='left' )
sV_26 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_26.set(80)
sV_26.pack(side='left' )
sV_27 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_27.set(80)
sV_27.pack(side='left' )
sV_28 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_28.set(80)
sV_28.pack(side='left' )
sV_29 = tk.Scale(master, from_=0, to=120,  length=720,tickinterval=20, orient=tk.VERTICAL)
sV_29.set(80)
sV_29.pack(side='left' )
master.mainloop()


当然, 4K 的屏幕才能完全显示这么多滑块




如果在普通的屏幕上 (1920x1080) 恐怕还是得调整。


## from tkinter import *
import tkinter as tk
def show_values():
    print (wSH1.get())
    print (sV_1.get(), sV_2.get(),sV_3.get(),sV_4.get(),sV_5.get(),sV_6.get(),sV_7.get(),
    sV_8.get(),sV_9.get(),sV_10.get(),sV_11.get(),sV_12.get(),sV_13.get(),sV_14.get(),sV_15.get(),
    sV_16.get(),sV_17.get(),sV_18.get(),sV_19.get(),sV_20.get(),sV_21.get(),sV_22.get(),sV_23.get(),
    sV_24.get(),sV_25.get(),sV_26.get(),sV_27.get(),sV_28.get(),sV_29.get() )
master = tk.Tk()
master.resizable(1, 1)
## tk.Button (master, text='Show', command=show_values).pack(side= tk.LEFT and 'bottom' )
tk.Button (master, text='Show', command=show_values).pack( )
wSH1 = tk.Scale(master, from_=0, to=100, length=1200,tickinterval=10, orient=tk.HORIZONTAL)
wSH1.set(32)
# wSH1.pack(side='top' )
wSH1.pack()
sV_1 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_1.set(80)
sV_1.pack(side='left' )
sV_2 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_2.set(80)
sV_2.pack(side='left' )
sV_3 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_3.set(80)
sV_3.pack(side='left' )
sV_4 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_4.set(80)
sV_4.pack(side='left' )
sV_5 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_5.set(80)
sV_5.pack(side='left' )
sV_6 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_6.set(80)
sV_6.pack(side='left' )
sV_7 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_7.set(80)
sV_7.pack(side='left' )
sV_8 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_8.set(80)
sV_8.pack(side='left' )
sV_9 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_9.set(80)
sV_9.pack(side='left' )
sV_10 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_10.set(80)
sV_10.pack(side='left' )
sV_11 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_11.set(80)
sV_11.pack(side='left' )
sV_12 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_12.set(80)
sV_12.pack(side='left' )
sV_13 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_13.set(80)
sV_13.pack(side='left' )
sV_14 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_14.set(80)
sV_14.pack(side='left' )
sV_15 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_15.set(80)
sV_15.pack(side='left' )
sV_16 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_16.set(80)
sV_16.pack(side='left' )
sV_17 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_17.set(80)
sV_17.pack(side='left' )
sV_18 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_18.set(80)
sV_18.pack(side='left' )
sV_19 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_19.set(80)
sV_19.pack(side='left' )
sV_20 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_20.set(80)
sV_20.pack(side='left' )
sV_21 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_21.set(80)
sV_21.pack(side='left' )
sV_22 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_22.set(80)
sV_22.pack(side='left' )
sV_23 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_23.set(80)
sV_23.pack(side='left' )
sV_24 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_24.set(80)
sV_24.pack(side='left' )
sV_25 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_25.set(80)
sV_25.pack(side='left' )
sV_26 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_26.set(80)
sV_26.pack(side='left' )
sV_27 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_27.set(80)
sV_27.pack(side='left' )
sV_28 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_28.set(80)
sV_28.pack(side='left' )
sV_29 = tk.Scale(master, from_=0, to=120, width=10, length=480,tickinterval=0, orient=tk.VERTICAL)
sV_29.set(80)
sV_29.pack(side='left' )
master.mainloop()


参考

Tkinter 8.5 reference: a GUI for Python
John W. Shipman  2013-06-24 12:46
https://reu.cct.lsu.edu/documents/Python_Course/tkinter.pdf
https://www.programcreek.com/python/example/105564/tkinter.Scale


练习 1


##
## FROM https://stackoverflow.com/questions/51842918/tkinter-pass-multiple-scale-value-to-a-label
import tkinter as tk
window = tk.Tk()
l1 = tk.Label(window, bg='pink', width=50, text='empty'+'\n'+'empty')
l1.pack()
def edited(event):
    l1.config(text='You have selected ' + str(s1.get()) +' trucks'+"\n"+"Truck Capacity is: "+  str(s2.get()) + " kg\n" + "Total Capacity is: \t" + str( s1.get()* s2.get() ) + " kg\n"     )
    #               ---------------------text1------------------+newline+---------------------text2--------------------
s1 = tk.Scale(window, label='Number of 7.5t truck', from_=0, to=30, orient=tk.HORIZONTAL,
             length=400, showvalue=0, tickinterval=5, resolution=1, command=edited)
s1.pack()
s2 = tk.Scale(window, label='Number of 12t truck', from_=0, to=30, orient=tk.HORIZONTAL,
             length=400, showvalue=0, tickinterval=5, resolution=1, command=edited)
s2.pack()
tk.mainloop()


https://zetcode.com/python/lambda/



Tkinter Passing Variables Between Windows

from tkinter import *
class Frames(object):
    def newFrame(self):
        newwin = Toplevel(root)
        newwin.title('New Window')
        newwin.geometry("320x240") 
        newwin.resizable(0, 0)
        display = Label(newwin, text="Hello, " + self.query.get()) #getting parameter via query var
        display.pack()
    def mainFrame(self,root):
        self.query = StringVar() #passing parameter via query var
        root.title('Open Window!!!')
        root.geometry("600x400") 
        root.resizable(1, 1)
        button1 =Button(root, text ="Open and Send New Window", command =self.newFrame)
        button1.place(x = 50, y = 25, width=400, height=45)
        entry1 = Entry(root, textvariable=self.query)
        entry1.place(x = 50, y = 175, width=200, height=45)
root = Tk()
app = Frames()
app.mainFrame(root)
root.mainloop()



OPEN FILE MENU


from tkinter import *
## from tkFileDialog import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.wm_title("Pages to PDF")
w = Label(root, text="Please choose a .pages file to convert.") 
fileName = askopenfilename(parent=root)
w.pack()
root.mainloop()



# https://stackoverflow.com/questions/22213600/nameerror-name-tkfiledialog-is-not-defined
    import Tkinter as tk
    import tkFileDialog as fd
except:
    import tkinter as tk
    from tkinter import filedialog as fd
def NewFile():
    print("New File!")
def OpenFile():
    name = fd.askopenfilename()
    print(name)
def About():
    print("This is a simple example of a menu")
class myGUI:
    def __init__(self, root):
        self.root = root
        self.canvas = tk.Canvas(self.root,
                                borderwidth=1,
                                relief="sunken")
        self.canvas.pack( fill=tk.BOTH, expand=tk.YES)
        self.menu = tk.Menu(self.root)
        self.root.config(menu=self.menu)
        self.helpmenu = tk.Menu(self.menu)
        self.filemenu = tk.Menu( self.menu )
        self.menu.add_cascade(label="File", menu=self.filemenu)
        self.filemenu.add_command(label="New", command=NewFile)
        self.filemenu.add_command(label="Open...", command=OpenFile)
        self.filemenu.add_separator()
        self.filemenu.add_command(label="Exit", command=root.destroy)
root = tk.Tk()   
root.title('appName')
myGUI(root)
root.mainloop()    







PLOT FFT

from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
saml_rate = 48000
N_frame = 1024*1024
sig_amp = 1.0 * np.sqrt(2)
sig_freq = 3000.0
noise_power = 0.000001 * saml_rate / 2
sig_duration = np.arange(N_frame) / saml_rate
x_float_stream = sig_amp * np.sin(2 * np.pi * sig_freq * sig_duration)
x_float_stream += np.random.normal(scale=np.sqrt(noise_power), size=sig_duration.shape)
# np.fft.fft
sig_freqs = np.fft.fftfreq(sig_duration.size, 1/saml_rate)
idx = np.argsort(sig_freqs)
ps = np.abs(np.fft.fft(x_float_stream))**2
plt.figure()
plt.plot(sig_freqs[idx], ps[idx])
plt.title('Power Spectrum (np.fft.fft)')
# signal.welch
f, Pxx_spec = signal.welch(x_float_stream, saml_rate, 'blackmanharris', 32768, scaling='spectrum')
plt.figure()
plt.semilogy(f, np.sqrt(Pxx_spec))
plt.xlabel('Sig Frequency [Hz]')
plt.ylabel('Linear spectrum [Magnitude Volt]')
plt.title('Power Spectrum (scipy.signal.welch)')
plt.show()







Plot FFT using Python – FFT of sine wave & cosine wave
https://www.gaussianwaves.com/2020/01/how-to-plot-fft-in-python-fft-of-basic-signals-sine-and-cosine-waves/
Applying Fourier Transform In Python Using Numpy.fft
https://pythontic.com/visualization/signals/fouriertransform_fft



# Python example - Fourier transform using numpy.fft method
import numpy as np
import matplotlib.pyplot as plotter
# How many time points are needed i,e., Sampling Frequency
samplingFrequency   = 48000;
# At what intervals time points are sampled
samplingInterval       = 1 / samplingFrequency;
# Begin time period of the signals
beginTime           = 0;
# End time period of the signals
endTime             = 0.1; 
# Frequency of the signals
signal1Frequency     = 200;
signal2Frequency     = 6000;
# Time points
time        = np.arange(beginTime, endTime, samplingInterval);
# Create two sine waves
amplitude1 = np.sin(2*np.pi*signal1Frequency*time)
amplitude2 = np.sin(2*np.pi*signal2Frequency*time)
# Create subplot
figure, axis = plotter.subplots(4, 1)
plotter.subplots_adjust(hspace=1)
# Time domain representation for sine wave 1
axis[0].set_title('Sine wave with a frequency of 200 Hz')
axis[0].plot(time, amplitude1)
axis[0].set_xlabel('Time')
axis[0].set_ylabel('Amplitude')
# Time domain representation for sine wave 2
axis[1].set_title('Sine wave with a frequency of 6000 Hz')
axis[1].plot(time, amplitude2)
axis[1].set_xlabel('Time')
axis[1].set_ylabel('Amplitude')
# Mix the sine waves
## amplitude = (0.5*amplitude1 + 1.0) * amplitude2  ## AM
amplitude = amplitude1 + amplitude2  ## ADDING
# Time domain representation of the resultant sine wave
axis[2].set_title('Sine wave with multiple frequencies')
axis[2].plot(time, amplitude)
axis[2].set_xlabel('Time')
axis[2].set_ylabel('Amplitude')
# Frequency domain representation
fourierTransform = np.fft.fft(amplitude)/len(amplitude)           # Normalize amplitude
fourierTransform = fourierTransform[range(int(len(amplitude)/2))] # Exclude sampling frequency
tpCount     = len(amplitude)
values      = np.arange(int(tpCount/2))
timePeriod  = tpCount/samplingFrequency
frequencies = values/timePeriod
# Frequency domain representation
axis[3].set_title('Fourier transform depicting the frequency components')
axis[3].plot(frequencies, abs(fourierTransform))
axis[3].set_xlabel('Frequency')
axis[3].set_ylabel('Amplitude')
plotter.show()



十年新的老问题

to pass arguments to a Button command in Tkinter?
https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter
https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter
https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter